Glassfish: JTA/JPA transaction not rolling back

╄→гoц情女王★ 提交于 2019-12-11 07:54:53

问题


I am running Glassfish 3.1.1 with an Oracle database and have run into an issue with transactions not rolling back, but only on one specific environment so far. The same application works as expected on other machines. However, two separate Glassfish domains on the same machine are impacted.

Within the affected environment, I have similar results with both a container-managed transactions (CMT) inside an EJB that throws a RuntimeException, and a bean-managed transaction (BMT) with UserTransaction#rollback().

In both cases, the underlying issue appears to be that the JDBC connection is somehow still set with autoCommit = true even though there is a JTA transaction in progress.

My EJB/CMT test looks like this:

@Named
@Stateless
public class TransactionTest { 

  @PersistenceContext
  EntityManager entityManager;

  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public void rollbackTest() {
    Foo foo = new Foo();
    entityManager.persist(foo);
    entityManager.flush();

    throw new RuntimeException("should be rolled back");
  }
}

and my BMT/UserTransaction test is like this:

public void rollbackUtxTest() throws Exception {
    utx.begin();

    Foo foo = new Foo();
    entityManager.persist(foo);
    entityManager.flush();

    utx.rollback();   
}

When I call either method, the INSERT INTO FOO is committed, even though the transactions were rolled back.

What am I missing - perhaps I don't have my connection pool / datasource is not set up right?

I'm using OracleConnectionPoolDataSource as the datasource class name. Is there something I need to do to ensure my database connections participate in JTA transactions?

UPDATE 1 I originally thought this was an issue with OracleConnectionPoolDataSource but it turned out it was not correlated. The same exact pool configuration works on one environment but not the other.

UPDATE 2 Clarified that this is not specifically an EJB/CMT issue, but a general JTA issue.

UPDATE 3 added info about JDBC autocommit. Confirmed that persistence.xml is correct.


回答1:


It looks like this may be an issue with domain.xml, possibly a Glassfish bug.

In persistence.xml, I have

<jta-data-source>jdbc/TEST</jta-data-source>.

In domain.xml, I have

<jdbc-resource pool-name="TEST_POOL" description="" jndi-name="jdbc/TEST"></jdbc-resource>

But no corresponding <resource-ref ref="jdbc/TEST"> - either missing or misspelled. (I believe I ended up in that state by creating the JNDI datasource through the UI, realizing the name is wrong, then fixing the JNDI name in domain.xml jdbc-resource by hand but not fixing it in resource-ref.

In this case, my injected EntityManager still works but is not participating in JTA transactions. If I fix domain.xml, it works as expected.




回答2:


You didn't wrap your Exception in an EJBException.

See http://docs.oracle.com/javaee/6/tutorial/doc/bnbpj.html



来源:https://stackoverflow.com/questions/8761922/glassfish-jta-jpa-transaction-not-rolling-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!