我正在向现有的 Spring/Hibernate Web 应用程序添加 JMS 功能(通过 ActiveMQ)。因此,我想向这个应用程序添加 XA/JTA 功能,以便我可以管理跨越数据存储和消息代理的事务(使用 Spring)。
我在 ActiveMQ 集成方面取得了成功,但是在将 XA/JTA 合并到应用程序中时遇到了问题。具体来说,没有数据被插入/更新到底层数据库中(不过,我在事务期间没有看到错误/异常)。
详细信息:Spring 3.1.2.RELEASE、Hibernate 3.6.10.Final、ActiveMQ 5.6.0
我正在使用 Spring Data JPA 来编写我的 DAO 接口,然后他们会拿起我的 LocalContainerEntityManagerFactoryBean 来完成他们的 CRUD 工作。
我已经尝试过 Atomikos (5.6.0) 和 Bitronix (2.1.3) XA/JTA 实现。每个都会导致相同的行为(没有数据写入基础数据库表)。
我有很多日志记录,如果需要,当然可以提供日志摘录。而且,配置细节也是如此。日志中特别值得注意的是,Spring 似乎表明 JTA 事务正在(试图?)提交。但是,鉴于缺少写入底层数据库的数据,尚不清楚它是否真的完成了提交(不过,我再次看到没有错误表明它没有完成)。此外,值得注意的是,此特定事务 - populateSampleData - 仅涉及数据库资源(根本不涉及消息代理)。
2012-08-14 13:16:09,770 DEBUG [org.springframework.transaction.annotation.AnnotationTransactionAttributeSource] - Adding transactional method 'populateSampleData' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-14 13:16:09,772 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,776 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Creating new transaction with name [com.teg.saamweb.service.SaamWebServiceTransactional.populateSampleData]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-14 13:16:09,811 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Opening JPA EntityManager
2012-08-14 13:16:09,812 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Registering transaction synchronization for JPA EntityManager
2012-08-14 13:16:09,898 DEBUG [org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-14 13:16:09,899 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,899 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:09,964 DEBUG [org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-14 13:16:09,964 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,964 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:09,982 DEBUG [org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] - Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-14 13:16:09,982 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,983 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:09,992 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,992 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:09,996 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:09,996 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,000 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,000 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,003 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,003 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,007 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,007 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,011 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,011 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,014 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,014 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,017 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,017 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,020 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,020 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,023 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,023 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,026 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,026 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,030 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,030 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,033 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,033 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,036 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] - Returning cached instance of singleton bean 'transactionManager'
2012-08-14 13:16:10,036 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Participating in existing transaction
2012-08-14 13:16:10,038 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
2012-08-14 13:16:10,039 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] - Initiating transaction commit
我希望找到一个成功地为事务管理设置了这样一个 Spring XA/JTA 应用程序的人,他可以就我可能做错了什么(或遗漏)提供一些建议。我基本上遵循了本文中的规定(在 Atomikos 案例中从 Atomikos 网站收集了一些附加信息)。
感谢您提供的任何启发。
更新:我解决了我的问题。问题是我没有使用 persistence.xml 文件(而是在使用 setPackagesToScan 方法设置 LocalContainerEntityManagerFactoryBean 时以编程方式配置持久性单元)。虽然持久性单元确实正确加载了我的@Entity 类,但由于缺少persistence.xml 文件(特别是缺少持久性单元元素的事务类型属性),事务类型被默认为RESOURCE_LOCAL(这是非 Java EE 容器的默认值)。我现在明确地提供了一个 persistence.xml 文件并明确地设置了 transaction-type="JTA"。