浏览 9528 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-12-11
Atomikos,是一个基于Java的开源事务管理器,提供了事务管理和连接池,不需要应用服务器支持,支持JDBC和JMS事务,能提供对Spring,Hibernate的集成,有兴趣多了解的可以直接参考说明文当,有详细的介绍和使用说明。 Atomikos可以到http://www.atomikos.com/products.html#ate下载,最新版本是3.2.0 首先,仍然是一个相关的配置文件,用来设置相关的环境,打开解压缩后Atomikos根目录,拷贝jta.properties到classpath中,并修改为如下内容 jta.properties com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory com.atomikos.icatch.console_file_name = tm.out com.atomikos.icatch.log_base_name = tmlog com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm com.atomikos.icatch.console_log_level = INFO 随后,是Spring上下文配置,建立一个名为ApplicationContext-atomikos.xml的Spring上下文 ApplicationContext-atomikos.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- XA Datasource --> <bean id="datasource1" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>mysql/main</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaDataSourceProperties"> <value>URL=jdbc:mysql://localhost:3306/test;user=root;password=admin</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>3</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <bean id="datasource2" class="com.atomikos.jdbc.SimpleDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>mysql/news</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</value> </property> <property name="xaDataSourceProperties"> <value>URL=jdbc:mysql://localhost:3306/test2;user=root;password=admin</value> </property> <property name="exclusiveConnectionMode"> <value>true</value> </property> <property name="connectionPoolSize"> <value>3</value> </property> <property name="validatingQuery"> <value>SELECT 1</value> </property> </bean> <bean id="template1" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource1" /> </bean> <bean id="template2" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="datasource2" /> </bean> <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown"><value>true</value></property> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300"/> </bean> <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"><ref bean="atomikosTransactionManager" /></property> <property name="userTransaction"><ref bean="atomikosUserTransaction" /></property> </bean> <bean id="dao1" class="com.xa.dao.UserDao1"> <property name="jdbcTemplate"> <ref bean="template1"></ref> </property> </bean> <bean id="dao2" class="com.xa.dao.UserDao2"> <property name="jdbcTemplate"> <ref bean="template2"></ref> </property> </bean> <!-- --> <bean id="userServiceTarget" class="com.xa.service.UserServiceImpl"> <property name="dao1" ref="dao1"/> <property name="dao2" ref="dao2"/> </bean> <bean id="userTest" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="springTransactionManager"/> </property> <property name="target"> <ref bean="userServiceTarget"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop> </props> </property> </bean> </beans> 看上下文中的代码,很大部分和JOTM的相似,atomikos使用com.atomikos.jdbc.SimpleDataSourceBean类来创建datasource bean,这里需要特别注意 <property name="uniqueResourceName"> <value>mysql/main</value> </property> <property name="uniqueResourceName"> <value>mysql/news</value> </property> 上面的代码为每个Datasource配置了一个唯一的名称,名称不允许相同,否则将会使用同一个datasource,而不会出现异常。 另外在配置文件中定义了Atomikos的事务管理器,并在Spring的JtaTransactionManager中引用 <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close"> <property name="forceShutdown"><value>true</value></property> </bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp"> <property name="transactionTimeout" value="300"/> </bean> <bean id="springTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="transactionManager"><ref bean="atomikosTransactionManager" /></property> <property name="userTransaction"><ref bean="atomikosUserTransaction" /></property> </bean> 其它的代码没什么特别,和JOTM配置基本相同,JdbcTemplate,DAO,Service,事务代理等等.... 仍然使用TestXa.java,修改成引用ApplicationContext-atomikos.xml的上下文配置 protected String[] getConfigLocations() { //return new String[] { "classpath:ApplicationContext-jotm.xml" }; return new String[] { "classpath:ApplicationContext-atomikos.xml" }; } 运行test,然后查看数据库状态, 然后修改方法参数使其不违反数据库约束,再运行Test观察数据库 :)。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-07-02
我按照你的配置了一些,我换成了hibernate3.2.6,为什么报下面这个错误:
2008-07-02 14:22:24,343 DEBUG [main] (BatchingBatcher.java:44) - Executing batch size: 10 2008-07-02 14:22:24,359 DEBUG [main] (AbstractBatcher.java:374) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 2008-07-02 14:22:24,359 DEBUG [main] (AbstractPlatformTransactionManager.java:893) - Triggering beforeCompletion synchronization 2008-07-02 14:22:24,359 DEBUG [main] (TransactionSynchronizationManager.java:199) - Removed value [org.springframework.orm.hibernate3.SessionHolder@b20352] for key [org.hibernate.impl.SessionFactoryImpl@578073] from thread [main] 2008-07-02 14:22:24,359 DEBUG [main] (AbstractPlatformTransactionManager.java:848) - Initiating transaction rollback after commit exception java.lang.IllegalArgumentException: null source at java.util.EventObject.<init>(Unknown Source) at javax.sql.StatementEvent.<init>(Unknown Source) at com.mysql.jdbc.jdbc2.optional.JDBC4PreparedStatementWrapper.close(JDBC4PreparedStatementWrapper.java:70) at org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:534) at org.hibernate.jdbc.AbstractBatcher.closeStatement(AbstractBatcher.java:269) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:135) at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:72) at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:882) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:692) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:678) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy7.addMySpringObject(Unknown Source) at myspring.service.impl.MySpringServiceTest.main(MySpringServiceTest.java:26) 2008-07-02 14:22:24,484 DEBUG [main] (AbstractPlatformTransactionManager.java:922) - Triggering afterCompletion synchronization 2008-07-02 14:22:24,484 DEBUG [main] (SessionFactoryUtils.java:774) - Closing Hibernate Session 2008-07-02 14:22:24,484 DEBUG [main] (ConnectionManager.java:441) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] Exception in thread "main" java.lang.IllegalArgumentException: null source at java.util.EventObject.<init>(Unknown Source) at javax.sql.StatementEvent.<init>(Unknown Source) at com.mysql.jdbc.jdbc2.optional.JDBC4PreparedStatementWrapper.close(JDBC4PreparedStatementWrapper.java:70) at org.hibernate.jdbc.AbstractBatcher.closePreparedStatement(AbstractBatcher.java:534) at org.hibernate.jdbc.AbstractBatcher.closeStatement(AbstractBatcher.java:269) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)2008-07-02 14:22:24,531 DEBUG [main] (ConnectionManager.java:302) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 2008-07-02 14:22:24,531 DEBUG [main] (TransactionSynchronizationManager.java:282) - Clearing transaction synchronization at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.springframework.orm.hibernate3.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:135) at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:72) at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:882) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:692) at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:678) at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy7.addMySpringObject(Unknown Source) at myspring.service.impl.MySpringServiceTest.main(MySpringServiceTest.java:26) |
|
返回顶楼 | |