In JBoss/WildFly should I enable JTA on data source to use with JPA?

后端 未结 3 898
悲哀的现实
悲哀的现实 2021-01-06 07:05

In JBoss/WildFly, when configuring a data source, there is a JTA option, which is disabled by default:



        
相关标签:
3条回答
  • 2021-01-06 07:43

    I've just experienced a problem related to this issue.

    I was running a container managed transaction involving approximately 20,000 inserts into a MySQL database.

    The transaction failed randomly, sometimes after around 3,500 inserts, other times after around 6,000 inserts, etc.

    After investigation I found that the JTA option on the WildFly datasource definition was set to false.

    Changing this setting to true fixed the problem, so I would agree with @user3472929 that JTA should be set to true in the datasource definition unless you have some specific reason not to.

    0 讨论(0)
  • 2021-01-06 07:53

    I think you should use jta. And if you set jta to false in the container configuration file, jta will be disabled for JPA, so the transaction-type for JPA will be "RESOURCE_LOCAL", which has some nasty side effect. By the way, jta is true in the container configuration file by default.

    0 讨论(0)
  • 2021-01-06 07:58

    Yes, of course you need to enable JTA on a datasource if you want to have jta transactions!

    Your XML/JBoss/Wildfly config file will look like this:

    <datasource jta="true" ...
    

    In our webapp persistence-unit, the datasource looks like this:

    <jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>
    

    The transaction-type="JTA" isn't necessary, at least not in my setup (Wildfly 8.1).

    In your Java code you can go like this to use transactions:

    @TransactionManagement(TransactionManagementType.CONTAINER) // class level
    public class ...
    ...
        @PersistenceContext(unitName = "CoreJPA")
        EntityManager em;
    
        @Resource
        private EJBContext ejbContext;
    ...
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // method level
    public void doSomething(...)
    

    And if you need to rollback, you do this:

    try {
      ...
    } catch (Throwable t) {
        log.error("Exception in create work order: " + t.getMessage());
        ejbContext.setRollbackOnly();
        throw t;
    }
    

    There are a lot of resources about this that can be found using Google.

    0 讨论(0)
提交回复
热议问题