Spring boot + Spring Data JPA + Atomikos + Multiple databases configuration

假装没事ソ 提交于 2019-12-02 17:41:56

I think the CMTTransaction in the stack trace is the clue: Hibernate thinks you are in a container. I also think it might work if you just remove the javax.persistence.transactionType=JTA property.

Ofcource, you can remove javax.persistence.transactionType=JTA and it will work.

But if you want to use JBOSS AS with own transaction manager (i like it more; there is a some bugs in atomicos which has fixed only customer version), you must put:

transactionType=JTA

Into persistance.xml property:

<property name="hibernate.transaction.jta.platform"
                      value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />

Into spring-config:

<tx:jta-transaction-manager/>
<tx:annotation-driven/>

https://blog.hanqunfeng.com/2016/12/21/spring-boot-study-jpa/#more

@Bean(name = "entityManagerFactory") @DependsOn({"atomikosJtaPlatfom"}) //需要先注册atomikosJtaPlatfom public LocalContainerEntityManagerFactoryBean entityManagerFactory() { System.out.println("entityManagerFactory init"); LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setJpaVendorAdapter(jpaVendorAdapter()); // entity package entityManager.setPackagesToScan("com.example.model.ds1"); entityManager.setJtaDataSource(dataSource()); Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); properties.put("hibernate.show_sql", "true"); properties.put("hibernate.format_sql", "true"); //jta设置 properties.put("hibernate.current_session_context_class", "jta"); properties.put("hibernate.transaction.factory_class", "org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory"); //这里指定我们自己创建的AtomikosJtaPlatfom properties.put("hibernate.transaction.jta.platform","com.example.AtomikosJtaPlatfom"); entityManager.setJpaProperties(properties); return entityManager; }

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