问题描述
项目需要连接分布式数据库,采用了Atomikos3.7.0+spring3.2.0+hibernate4.2.0。目前能够连接到数据库,但是无法进行事务的提交,下面贴上配置:spring.xml<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"default-lazy-init="true"><!--从jdbc.properties中获取jdbc参数--><!--从genericCatch.properties获取需要异常捕获处理的controller的方法--><beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:/jdbc.properties</value></list></property></bean><importresource="mvc.xml"/><importresource="spring-dao.xml"/></beans>
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><!--数据源配置================开始====================--><beanid="dataSource"class="com.atomikos.jdbc.AtomikosDataSourceBean"init-method="init"destroy-method="close"><propertyname="uniqueResourceName"value="oracle/dgg_heshun"/><propertyname="xaDataSourceClassName"value="${jdbc.driverClassName}"/><propertyname="xaProperties"><props><propkey="URL">${jdbc.url}</prop><propkey="user">${jdbc.username}</prop><propkey="password">${jdbc.password}</prop></props></property><propertyname="poolSize"value="${cpool.minPoolSize}"/><propertyname="maxPoolSize"value="${cpool.maxPoolSize}"/><propertyname="testQuery"value="select1fromdual"/></bean><!--数据源配置================结束====================--><!--SessionFactory配置================开始====================--><beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!--dataSource属性指定要用到的连接池--><propertyname="dataSource"ref="dataSource"/><propertyname="packagesToScan"><list><value>com.jx</value></list></property><!--配置Hibernate的属性--><propertyname="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.OracleDialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=falsehibernate.temp.use_jdbc_metadata_defaults=false</value></property></bean><!--SessionFactory配置================结束====================--><!--分布式数据库事务管理================开始====================--><beanid="atomikosTransactionManager"class="com.atomikos.icatch.jta.UserTransactionManager"init-method="init"destroy-method="close"><propertyname="forceShutdown"value="true"/></bean><beanid="atomikosUserTransaction"class="com.atomikos.icatch.jta.UserTransactionImp"><propertyname="transactionTimeout"><value>300</value></property></bean><beanid="txManager"class="org.springframework.transaction.jta.JtaTransactionManager"><propertyname="transactionManager"ref="atomikosTransactionManager"/><propertyname="userTransaction"ref="atomikosUserTransaction"/></bean><tx:adviceid="advice"transaction-manager="txManager"><tx:attributes><tx:methodname="add*"propagation="REQUIRED"/></tx:attributes></tx:advice><aop:config><aop:pointcutid="point"expression="execution(*add*(..))"/><aop:advisoradvice-ref="advice"pointcut-ref="point"/></aop:config><!--分布式数据库事务管理================结束====================--></beans>
简单的包:启动时候看起来没有问题,在提交事务的时候,控制台打印了如下信息:16:53:46.419[http-bio-8080-exec-1]DEBUGorg.hibernate.SQL-selecthibernate_sequence.nextvalfromdualHibernate:selecthibernate_sequence.nextvalfromdual16:53:46.420[http-bio-8080-exec-1]DEBUGo.h.e.j.i.LogicalConnectionImpl-ObtainingJDBCconnection16:53:46.420[http-bio-8080-exec-1]INFOatomikos-AtomikosDataSoureBean'oracle/dgg_heshun':getConnection(null)...16:53:46.420[http-bio-8080-exec-1]INFOatomikos-AtomikosDataSoureBean'oracle/dgg_heshun':init...16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.421[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.447[http-bio-8080-exec-1]DEBUGo.h.e.j.i.LogicalConnectionImpl-ObtainedJDBCconnection16:53:46.447[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.447[http-bio-8080-exec-1]DEBUGatomikos-getCompositeTransaction()returningNULL!16:53:46.447[http-bio-8080-exec-1]INFOatomikos-atomikosconnectionproxyfororacle.jdbc.driver.LogicalConnection@4ecfe790:callingprepareStatement...16:53:46.460[http-bio-8080-exec-1]DEBUGorg.hibernate.id.SequenceGenerator-Sequenceidentifiergenerated:BasicHolder[java.lang.Long[84283]]16:53:46.462[http-bio-8080-exec-1]DEBUGo.h.e.i.AbstractSaveEventListener-Generatedidentifier:84283,usingstrategy:org.hibernate.id.SequenceGenerator网上找了很久没有好的办法,采用了注解方式来配置事务也没有用处,请各位帮忙看看到底问题出在哪里了
解决方案
解决方案二:
额。。我之前用aop配置事务也不生效后来用的注解我不知道你注解的话是如何配置的
解决方案三:
引用1楼darrenzhong的回复:
额。。我之前用aop配置事务也不生效后来用的注解我不知道你注解的话是如何配置的
如下:<!--定义Hibernate事务管理器--><beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"/></bean><!--开启注解事务定义,由Spring扫描注解定义的事务--><tx:annotation-driventransaction-manager="transactionManager"/>
java代码里:@Transactional(propagation=Propagation.REQUIRED)publicinterfaceTestDmnextendsGenericDmn<Test,Long>{}
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)publicinterfaceGenericDmn<T,IDextendsSerializable>{@Transactional(propagation=Propagation.REQUIRED)Tadd(Tentity)throwsBusinessException;}
这样配置之后,就没有打印信息了,但是依然无法提交事务
解决方案四:
有打印出数据库操作的sql日志吗
解决方案五:
引用3楼dokia123的回复:
有打印出数据库操作的sql日志吗
打印了:Hibernate:selecthibernate_sequence.nextvalfromdual但是没有insert语句。
解决方案六:
现在发现使用junit的时候,可以进行提交事务,但是使用web容器之后,就无法提交事务了。好像是tomcat要支持jta,需要加另外一个包,马上去试试。
解决方案七:
分布式事务你还采用本地事务管理器?改成JTATranscationManager,把tomcat改成应用服务器。
解决方案八:
楼主解决了没,我也遇到配置后事务不可用的情况。
解决方案九:
楼主,事务不提交的事情解决了没?
解决方案十:
引用7楼u010612285的回复:
楼主解决了没,我也遇到配置后事务不可用的情况。
我的这事务也没提交,你的解决了没?
解决方案十一:
同问……解决了,回一个……谢谢……
解决方案十二:
用hadoop或者MPP数据库吧,那个处理起来非常方便。