最近一直在测试 spring jta jotm ibatis的 事务配置,但是 由于
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice>
中 在ibatis上 如果是readonly的 执行插入操作还是能插入,故换到hibernate进行测试
为了以后方便记录下来配置文件方法:
在配置hibernate时候还有一个重要的问题就是 多数据源配置hibernate时候
Caused by: org.hibernate.cache.CacheException: Attempt to restart an already started EhCacheProvider.
Use sessionFactory.close() between repeated calls to buildSessionFactory.
Consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider.
---------------------
org.hibernate.cache.CacheException: Attempt to restart an already started EhCacheProvider.
Use sessionFactory.close() between repeated calls to buildSessionFactory.
Consider using net.sf.ehcache.hibernate.SingletonEhCacheProvider.
Error from ehcache was: Cannot parseConfiguration CacheManager.
Attempt to create a new instance of CacheManager using
the diskStorePath "C:\Tomcat5.0\temp" which is already used by an existing CacheManager.
The source of the configuration was classpath.
-----------------
不知道什么原因 后来不断更新几个与hibernate相关的required 的jar包版本 后可以正常运行,就没深入。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:comp/env/jdbc/manager_jdbc</value> </property> </bean> --> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/crm" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> --> <!-- <bean id="userCache" class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache"> <property name="cache"><ref local="userCacheBackend"/></property> </bean> <bean id="userCacheBackend" class="org.springframework.cache.ehcache.EhCacheFactoryBean"> <property name="cacheManager"><ref local="cacheManager"/></property> <property name="cacheName"><value>userCache</value></property> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"/> --> <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" /> <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction" ref="jotm" /> </bean> <bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/crm" /> </bean> </property> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <bean id="dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource" destroy-method="shutdown"> <property name="dataSource"> <bean class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="shutdown"> <property name="transactionManager" ref="jotm" /> <property name="driverName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1/crm2" /> </bean> </property> <property name="user" value="root" /> <property name="password" value="root" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/wsd/vo/hibernate</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">50</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> <property name="jtaTransactionManager"> <ref bean="jotm" /> </property> </bean> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> <bean id="sessionFactoryB" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource2" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/wsd/vo/hibernate</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.jdbc.batch_size">50</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> </props> </property> <property name="jtaTransactionManager"> <ref bean="jotm" /> </property> </bean> <!-- fenge --> <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1/crm"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> --> <!-- fenge --> <!-- <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.wsd.service.*.*(..))" /> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" /> </aop:config> --> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceOperation" expression="execution(* com.wsd.service.*.*(..))" /> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="select*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> --> <bean id="manSqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" autowire="byName"> <property name="configLocation"> <value>classpath:newpay-SqlMap-Config.xml</value> </property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="manSqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" autowire="byName"> <property name="configLocation"> <value>classpath:newpay-SqlMap-Config.xml</value> </property> <property name="dataSource" ref="dataSource2" /> </bean> </beans>
<?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.xsd"> <!-- 公共类的查询dao --> <bean id="testDao" class="com.wsd.dao.impl.TestDaoImpl" autowire="byName"></bean> <bean id="testDao2" class="com.wsd.dao.impl.TestDaoImpl2" autowire="byName"> <property name="sqlMapClient" ref="manSqlMapClient"></property> </bean> <!-- <bean id="testDao3" class="com.wsd.dao.impl.TestDaoImpl3" autowire="byName"> <property name="sessionFactory" ref="sessionFactoryA"></property> </bean> --> <bean id="testDao3" class="com.wsd.dao.impl.TestDaoImpl3"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> </beans>
public void testAddCustomer() throws Exception { TestService cm = (TestService)factory.getBean("testService"); for(int i=0; i<1; i++){ User u = new User(); u.setId(i+""); u.setName("name"+i); u.setPass("pass"+i); // cm.addUser(u); // cm.addUser2(u); // cm.doUser(u); // cm.doUser2(u); // cm.addUser3(u);//可以插入 cm.doUser3(u);//插入失败 } } //-------------------------- public void addUser3(User u) throws Exception { // this.testDao.addUser(u); // this.testDao.addUser2(u); this.testDao3.addUser(u); this.testDao3.addUser2(u); System.out.println("addUser3"); } public void doUser3(User u) throws Exception { // this.testDao.addUser(u); // this.testDao.addUser2(u); // this.testDao2.addUser(u); // this.testDao2.addUser2(u); this.testDao3.addUser(u); this.testDao3.addUser2(u); System.out.println("doUser3"); } /* org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186) at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696) at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419) at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694) at com.wsd.dao.impl.TestDaoImpl3.addUser(TestDaoImpl3.java:25) at com.wsd.service.impl.TestServiceImpl.doUser3(TestServiceImpl.java:89) at com.wsd.service.impl.TestServiceImpl$$FastClassByCGLIB$$cf702a21.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:700) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635) at com.wsd.service.impl.TestServiceImpl$$EnhancerByCGLIB$$14310820.doUser3(<generated>) at test.CustomerManagerTest.testAddCustomer(CustomerManagerTest.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) */