Tomcat下配置Atomikos实现JTA
Tomcat作为一款经典的Web服务器,在开发、测试和生产环境中得到了广泛的使用。但Tomcat毕竟不是Java EE服务器,因此在EJB,JTA方面并没有提供支持。本文讲述了Tomcat使用Atomikos实现JTA的一种方法。
在Tomcat中使用JTA,可以将Atomikos部署在Tomcat中,使用Tomcat支持的数据源;也可以在项目中配置,利用Spring配置好数据源、连接池、事务管理器等等。两种方式各有特点,本文只介绍Tomcat与Atomikos的集成,集成后Tomcat可以对外提供JTA的事务管理器和数据源。
在使用Atomikos之前,我们也曾使用过JOTM,不过在高并发的情况下,JOTM频频出错,最后不得不放弃,通过测试,发现Atomikos性能和稳定性都不错。
我们使用了Atomikos最新的4.04版本,Jar包的获取可以从maven的配置库中得到,链接地址:http://mvnrepository.com/artifact/com.atomikos
如果不使用Hibernate,需要的包包括:
atomikos-util.jar,jta.jar,transactions.jar,transactions-api.jar,transactions-jdbc.jar,
transactions-jta.jar
配置步骤如下:
Step 1:将这些jar 拷贝到tomcat 的lib 目录中。要实现Tomcat与Atomikos集成,还需要一个集成包,这个集成包里面有两个class,可以自己参考实现,也可以使用官方提供的jar包,最新的是atomikos-integration-extension-3.7.2.jar
Step2:在tomcat/config/server.xml中 增加一个监听器
<Listener className="com.atomikos.tomcat.AtomikosLifecycleListener" /> |
Step3:在tomcat/config/context.xml中增加数据源和相关的事务管理器,下面是一个参考的例子,参数酌情修改
<Resource name="jdbc/DS_MYSQL" auth="Container" type="com.atomikos.jdbc.AtomikosDataSourceBean" uniqueResourceName="jdbc/DS_MYSQL" xaDataSourceClassName="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" xaProperties.databaseName="db_test" xaProperties.serverName="localhost" xaProperties.port="3306" xaProperties.user="root" xaProperties.password="root" maxPoolSize="200" xaProperties.url="jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF8" factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory" /> <Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction" /> <Transaction factory="com.atomikos.icatch.jta.UserTransactionFactory" /> |
Step4:在tomcat/lib目录下增加一个jta.properties文件,设置Atomikos事务相关的参数,否则将使用默认的配置参数,一些并发事务数(默认50个),超时时间等都需要调整,下面给出了文件中的一些参数配置,参数解释请查阅官方文档:https://www.atomikos.com/Documentation/JtaProperties
# SAMPLE PROPERTIES FILE FOR THE TRANSACTION SERVICE # THIS FILE ILLUSTRATES THE DIFFERENT SETTINGS FOR THE TRANSACTION MANAGER # UNCOMMENT THE ASSIGNMENTS TO OVERRIDE DEFAULT VALUES; # Required: factory implementation class of the transaction core. # NOTE: there is no default for this, so it MUST be specified! # com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory # Set base name of file where messages are output # (also known as the 'console file'). # # com.atomikos.icatch.console_file_name = tm.out # Size limit (in bytes) for the console file; # negative means unlimited. # # com.atomikos.icatch.console_file_limit=-1 # For size-limited console files, this option # specifies a number of rotating files to # maintain. # # com.atomikos.icatch.console_file_count=1 # Set the number of log writes between checkpoints # # com.atomikos.icatch.checkpoint_interval=500 # Set output directory where console file and other files are to be put # make sure this directory exists! # # com.atomikos.icatch.output_dir = ./ # Set directory of log files; make sure this directory exists! # # com.atomikos.icatch.log_base_dir = ./ # Set base name of log file # this name will be used as the first part of # the system-generated log file name # # com.atomikos.icatch.log_base_name = tmlog # Set the max number of active local transactions # or -1 for unlimited. # # com.atomikos.icatch.max_actives = 50 # Set the default timeout (in milliseconds) for local transactions # # com.atomikos.icatch.default_jta_timeout = 10000 # Set the max timeout (in milliseconds) for local transactions # # com.atomikos.icatch.max_timeout = 300000 # The globally unique name of this transaction manager process # override this value with a globally unique name # # com.atomikos.icatch.tm_unique_name = tm # Do we want to use parallel subtransactions? JTA's default # is NO for J2EE compatibility # # com.atomikos.icatch.serial_jta_transactions=true # If you want to do explicit resource registration then # you need to set this value to false. # # com.atomikos.icatch.automatic_resource_registration=true # Set this to WARN, INFO or DEBUG to control the granularity # of output to the console file. # # com.atomikos.icatch.console_log_level=WARN # Do you want transaction logging to be enabled or not? # If set to false, then no logging overhead will be done # at the risk of losing data after restart or crash. # # com.atomikos.icatch.enable_logging=true # Should two-phase commit be done in (multi-)threaded mode or not? # Set this to false if you want commits to be ordered according # to the order in which resources are added to the transaction. # # NOTE: threads are reused on JDK 1.5 or higher. # For JDK 1.4, thread reuse is enabled as soon as the # concurrent backport is in the classpath - see # http://mirrors.ibiblio.org/pub/mirrors/maven2/backport-util-concurrent/backport-util-concurrent/ # # com.atomikos.icatch.threaded_2pc=false # Should shutdown of the VM trigger shutdown of the transaction core too? # # com.atomikos.icatch.force_shutdown_on_vm_exit=false |
Atomikos中参数的默认值在transactions.jar中定义,transactions-default.properties,具体参数如下:
com.atomikos.icatch.enable_logging=true com.atomikos.icatch.force_shutdown_on_vm_exit=false com.atomikos.icatch.automatic_resource_registration=true com.atomikos.icatch.checkpoint_interval=500 com.atomikos.icatch.serial_jta_transactions=true com.atomikos.icatch.default_jta_timeout=10000 com.atomikos.icatch.max_timeout=300000 com.atomikos.icatch.log_base_dir=./ com.atomikos.icatch.threaded_2pc=false com.atomikos.icatch.max_actives=50 com.atomikos.icatch.log_base_name=tmlog java.naming.factory.initial=com.sun.jndi.rmi.registry.RegistryContextFactory com.atomikos.icatch.client_demarcation=false java.naming.provider.url=rmi://localhost:1099 com.atomikos.icatch.rmi_export_class=none com.atomikos.icatch.trust_client_tm=false com.atomikos.icatch.forget_orphaned_log_entries_delay=86400000 com.atomikos.icatch.recovery_delay=${com.atomikos.icatch.default_jta_timeout} com.atomikos.icatch.oltp_max_retries=5 com.atomikos.icatch.oltp_retry_interval=10000 com.atomikos.icatch.allow_subtransactions=true |
配置完以上四个步骤,Tomcat的集成就算完成了,项目中可以使用Spring来关联数据源和事务管理器,参考配置如下:
<!-- JNDI模板配置信息,用于连接应用服务器--> <bean class="org.springframework.jndi.JndiTemplate" id="jndiTemplate" /> <!--引用Tomcat数据源--> <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="dataSource"> <property name="jndiName"> <value>java:comp/env/jdbc/DS_MYSQL</value> </property> <property name="jndiTemplate"> <ref bean="jndiTemplate"/> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> <!--用户事务对象--> <bean class="org.springframework.jndi.JndiObjectFactoryBean" id="userTransaction"> <!--class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">--> <property name="jndiName"> <value>java:comp/UserTransaction</value> </property> <property name="jndiTemplate"> <ref bean="jndiTemplate"/> </property> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown" value="false" /> </bean> <!-- 配置基于注解的声明式事务管理器 --> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="userTransaction" /> <property name="transactionManager" ref="atomikosTransactionManager" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> |
在Tomcat配置中使用的XA的数据源和JDBC驱动,应该可以使用nonXA的相关设置,Atomikos中也支持非XA的连接,以提高运行速度。关于nonXa的数据源,可以参考一下配置:
<Resource name="jdbc/DS_MYSQL" auth="Container" type="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean" uniqueResourceName="jdbc/DS_MYSQL" driverClassName="com.mysql.jdbc.Driver" maxPoolSize="200" url="jdbc:mysql://localhost:3306/db_test?characterEncoding=UTF8" user="root" password="root" factory="com.atomikos.tomcat.EnhancedTomcatAtomikosBeanFactory" /> |
本文中的内容参考官方文档整理:细节请查阅
https://www.atomikos.com/Documentation/Tomcat7Integration35