尝试使用Tomcat,Atomikos,ActiveMQ和MySql使事务正常工作。遵循以下示例: Tomcat 7 Integration with Atomikos 3.5.2 和Tomcat 7.0.27 Integration with Atomikos 3.7.1没有成功......
ERROR: com.atomikos.icatch.jta.UserTransactionManager cannot be cast to javax.transaction.TransactionManager
java.lang.ClassCastException: com.atomikos.icatch.jta.UserTransactionManager cannot be cast to javax.transaction.TransactionManager
使用:
在$ TOMCAT_HOME / lib
下删除了以下jar以下是transactions.properties
的内容com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.automatic_resource_registration=true
com.atomikos.icatch.output_dir=../work/atomikos
com.atomikos.icatch.log_base_dir=../work/atomikos/log
com.atomikos.icatch.enable_logging=true
com.atomikos.icatch.console_log_level=TRACE
context.xml中
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" />
<Resource name="TransactionManager"
auth="Container"
type="com.atomikos.icatch.jta.UserTransactionManager"
factory="org.apache.naming.factory.BeanFactory" />
<Resource name="UserTransaction"
auth="Container"
type="com.atomikos.icatch.jta.UserTransactionImp"
factory="org.apache.naming.factory.BeanFactory" />
<Resource name="jms/ConnectionFactory"
auth="Container"
description="JMS Connection Factory"
type="com.atomikos.jms.AtomikosConnectionFactoryBean"
factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory"
uniqueResourceName="jms/ConnectionFactory"
xaConnectionFactoryClassName="org.apache.activemq.ActiveMQXAConnectionFactory"
xaProperties.brokerURL="vm://localhost"
xaProperties.transportType="1"
localTransactionMode="true" />
<Resource name="jms/WsTopic"
auth="Container"
type="org.apache.activemq.command.ActiveMQTopic"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="WS.TOPIC" />
<Resource name="jms/WsQueue"
auth="Container"
type="org.apache.activemq.command.ActiveMQQueue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="WS.QUEUE" />
<!-- MySQL -->
<Resource name="jdbc/DB"
auth="Container"
type="com.atomikos.jdbc.AtomikosDataSourceBean"
factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory"
uniqueResourceName="jdbc/DB"
minPoolSize="5"
maxPoolSize="10"
testQuery="SELECT 1 FROM DUAL"
xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource"
xaProperties.databaseName="db"
xaProperties.serverName="localhost"
xaProperties.port="3306"
xaProperties.user="user"
xaProperties.password="password"
xaProperties.url="jdbc:mysql://localhost:3306/db"
xaProperties.pinGlobalTxToPhysicalConnection="true"
xaProperties.autoReconnect="true"
xaProperties.autoReconnectForConnectionPools="true"
xaProperties.autoReconnectForPools="true" />
</Context>
相关的java代码:
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
...
Hashtable<String, String> hashTable = new Hashtable<String, String>();
try {
Context ctx = new InitialContext(hashTable);
ctx = (Context) jndiContext.lookup("java:comp/env");
TransactionManager transactionManager = (TransactionManager) ctx.lookup("TransactionManager");
} catch (Exception e) {
e.printStackTrace();
}
我做错了什么?
修改
从$ TOMCAT_HOME / lib中删除activemq-all-5.8.0.jar后,我能够超越原始错误,但现在我得到了:
com.atomikos.jms.AtomikosJMSException: Error in proxy
at com.atomikos.jms.AtomikosJMSException.throwAtomikosJMSException(AtomikosJMSException.java:54)
at com.atomikos.jms.ConsumerProducerSupport.handleException(ConsumerProducerSupport.java:61)
at com.atomikos.jms.AtomikosJmsMessageConsumerProxy.receive(AtomikosJmsMessageConsumerProxy.java:73)
at com.atomikos.jms.AtomikosJmsMessageConsumerProxy.receive(AtomikosJmsMessageConsumerProxy.java:137)
...
at java.lang.Thread.run(Thread.java:619)
Caused by: com.atomikos.jms.AtomikosTransactionRequiredJMSException: The JMS session you are using requires a JTA transaction context for the calling thread and none was found.
Please correct your code to do one of the following:
1. start a JTA transaction if you want your JMS operations to be subject to JTA commit/rollback, or
2. increase the maxPoolSize of the AtomikosConnectionFactoryBean to avoid transaction timeout while waiting for a connection, or
3. create a non-transacted session and do session acknowledgment yourself, or
4. set localTransactionMode to true so connection-level commit/rollback are enabled.
at com.atomikos.jms.AtomikosTransactionRequiredJMSException.throwAtomikosTransactionRequiredJMSException(AtomikosTransactionRequiredJMSException.java:38)
at com.atomikos.jms.ConsumerProducerSupport.enlist(ConsumerProducerSupport.java:107)
at com.atomikos.jms.AtomikosJmsMessageConsumerProxy.receive(AtomikosJmsMessageConsumerProxy.java:70)
... 5 more
答案 0 :(得分:3)
Tomcat使用multiple class-loaders。请参阅their definitions,它是如何工作的,哪个优先(bootstrap,system,webapp,common)。我假设除了geronimo-jta_1.0.1B_spec-1.0.jar
之外,您还将transactions-jta-3.7.1.jar
和/或webapp.war/WEB-INF/lib/
置于WAR文件$TOMCAT_HOME/lib/
中。这可能会导致问题,因为javax.transaction.TransactionManager
类将被不同的类加载器多次加载。即使该类具有相同的名称,如果它由不同的类加载器加载,则转换也会失败。
当Tomcat初始化容器范围内的 TransactionManager 资源时,它首先加载了 common 类加载器($ TOMCAT_HOME / lib / * .jar)由context.xml
实施的com.atomikos.icatch.jta.UserTransactionManager
。 (Tomcat目前不使用任何 webapp 类加载器。)
您在Web应用程序中使用javax.transaction.TransactionManager
类,现在它将从位于{{的JAR文件的副本中加载 webapp 类加载器1}} WAR文件中的文件夹。
尝试从webapp.war/WEB-INF/lib/
中删除包含javax.transaction.TransactionManager
类的JAR文件,然后尝试重新部署您的应用程序。
或者,在webapp.war/WEB-INF/lib/
系统变量中列出这些JAR文件,因此这些类将由在Tomcat中具有优先权的系统类加载器加载。