Problemas com transação unica usando 2 entityManager

2 respostas
leosouzabh

Estou tentando executar um metodo que trabalhe com 2 banco de dados (2 entityManager)
este processo tem que ser envolvido por uma transação unica.

Como devo proceder....?!?!??
Vejam da forma que eu fiz:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public Integer reservaSmg(Formatacao formatacao, Integer totalSmg) throws AplicacaoException {
		try {
			
			LsTabSmgId lsTabSmgId = new LsTabSmgId();
			lsTabSmgId.setCliente   ( formatacao.getCodigoCliente() );
						
			Integer smgInicial = 0;
			Integer smgFinal   = totalSmg;
			LsTabSmg smg = (LsTabSmg) cppEm.find(LsTabSmg.class, lsTabSmgId);
			
			if (smg == null) {				
				smg = new LsTabSmg(lsTabSmgId, totalSmg);				
				cppEm.persist( smg );
			} else {
				smg.setSmg( smg.getSmg() + totalSmg );				
			}
			
			Formatacao fmtPersistido = formatacaoEm.find( Formatacao.class, formatacao.getIdFormatacao() );
			fmtPersistido.setSmgInicial(smgInicial);
			fmtPersistido.setSmgFinal(smgFinal);
			
			return smg.getSmg();
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new AplicacaoException();
		}
	}
desta forma quando ele executa o find do segundo entityManager é disparado a seguinte exception:
14:09:59,203 WARN  [loggerI18N] [com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.disallow] [com.arjuna.ats.internal.jta.transaction.arjunacore.lastResource.disallow] Adding multiple last resources is disallowed. Current resource is org.jboss.resource.connectionmanager.TxConnectionManager$LocalXAResource@c22e8e
14:09:59,281 WARN  [JDBCExceptionReporter] SQL Error: 0, SQLState: null
14:09:59,281 ERROR [JDBCExceptionReporter] Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: a020ab3:f1e:48064eab:bb status: ActionStatus.ABORT_ONLY >); - nested throwable: (org.jboss.resource.JBossResourceException: Could not enlist in transaction on entering meta-aware object!; - nested throwable: (javax.transaction.SystemException: java.lang.Throwable: Unabled to enlist resource, see the previous warnings. tx=TransactionImple < ac, BasicAction: a020ab3:f1e:48064eab:bb status: ActionStatus.ABORT_ONLY >))
14:09:59,297 INFO  [DefaultLoadEventListener] Error performing load command
org.hibernate.exception.GenericJDBCException: Cannot open connection
	at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
	at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:426)
	at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
	at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)

2 Respostas

L

Olá leosouzabh,

Precisei fazer algo parecido.
Utilizei o datasource XA para two-phase commit.
Não testei para dois bancos diferentes, mas teoricamente deveria funcionar.

Eu uso DB2. Mas se for outro, dá uma procurada no wiki do JBoss.

http://wiki.jboss.org/wiki/SetUpADB2Datasource

[]s
LISS

ceara

leosouzabh:
Estou tentando executar um metodo que trabalhe com 2 banco de dados (2 entityManager)
este processo tem que ser envolvido por uma transação unica.

Cara… tu conseguiu o que queria??

Estou estudando para tentar fazer o mesmo!

abraço

Criado 16 de abril de 2008
Ultima resposta 2 de jun. de 2008
Respostas 2
Participantes 3