http://418684644-qq-com.iteye.com/blog/1204565

 

首先引入jotm和xpool包,通过maven引入

 

Java代码  

  1.   

  2.             com.experlog   

  3.             xapool   

  4.             1.5.0   

  5.            

  6.            

  7.             org.ow2.jotm   

  8.             jotm-core   

  9.             2.2.2   

  10.            

  11.            

  12.             org.ow2.jotm   

  13.             jotm   

  14.             2.2.2   

  15.             pom   

  16.            

  17.            

  18.             org.ow2.spec.ee   

  19.             ow2-connector-1.5-spec   

  20.             1.0.9   

  21.           


			com.experlog
			xapool
			1.5.0
		
		
			org.ow2.jotm
			jotm-core
			2.2.2
		
		
			org.ow2.jotm
			jotm
			2.2.2
			pom
		
		
			org.ow2.spec.ee
			ow2-connector-1.5-spec
			1.0.9
		

 

spring配置

 

Java代码  

  1.   

  2. "jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />   

  3.   

  4.   

  5. "txManager"  

  6. class="org.springframework.transaction.jta.JtaTransactionManager">   

  7.     "userTransaction" ref="jotm" />   

  8.   

  9.   

  10.   

  11.     "dataSource1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"  

  12.         destroy-method="shutdown">   

  13.         "dataSource">   

  14.             class="org.enhydra.jdbc.standard.StandardXADataSource"  

  15.                 destroy-method="shutdown">   

  16.                 "transactionManager" ref="jotm">   

  17.                 "driverName" value="${jdbc.driver}">   

  18.                 "url" value="${jdbc.url1}">   

  19.                

  20.            

  21.         "user" value="${jdbc.username}" />   

  22.         "password" value="${jdbc.password}" />   

  23.        

  24.   

  25.     "dataSource2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"  

  26.         destroy-method="shutdown">   

  27.         "dataSource">   

  28.             class="org.enhydra.jdbc.standard.StandardXADataSource"  

  29.                 destroy-method="shutdown">   

  30.                 "transactionManager" ref="jotm">   

  31.                 "driverName" value="${jdbc.driver}">   

  32.                 "url" value="${jdbc.url2}">   

  33.                

  34.            

  35.         "user" value="${jdbc.username}" />   

  36.         "password" value="${jdbc.password}" />   

  37.        

  38. "sessionFactory1"  

  39.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   

  40.         "dataSource" ref="dataSource1" />   

  41.         "hibernateProperties">   

  42.                

  43.                 "hibernate.dialect">${hibernate.dialect}   

  44.                 "hibernate.show_sql">${hibernate.show_sql}   

  45.                 "hibernate.hbm2ddl.auto">${hibernate.auto}   

  46.                 "current_session_context_class">jta   

  47.                 "hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider   

  48.                 "hibernate.search.default.indexBase">F:/temp/index   

  49.                

  50.            

  51.         "annotatedClasses">   

  52.                

  53.                 com.zyn.ssh.pojo.Student   

  54.                 com.zyn.ssh.pojo.Teacher   

  55.                 com.zyn.ssh.pojo.Course   

  56.                 com.zyn.ssh.pojo.StudentInfo   

  57.                

  58.            

  59.        

  60.     "sessionFactory2"  

  61.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   

  62.         "dataSource" ref="dataSource2" />   

  63.         "hibernateProperties">   

  64.                

  65.                 "hibernate.dialect">${hibernate.dialect}   

  66.                 "hibernate.show_sql">${hibernate.show_sql}   

  67.                 "hibernate.hbm2ddl.auto">${hibernate.auto}   

  68.                 "current_session_context_class">jta   

  69.                 "hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider   

  70.                 "hibernate.search.default.indexBase">F:/temp/index   

  71.                

  72.            

  73.         "annotatedClasses">   

  74.                

  75.                 com.zyn.ssh.pojo.Student   

  76.                 com.zyn.ssh.pojo.Teacher   

  77.                 com.zyn.ssh.pojo.Course   

  78.                 com.zyn.ssh.pojo.StudentInfo   

  79.                

  80.            

  81.        

  82.   

  83.     "txManager"  

  84.         proxy-target-class="true" />  






	



	
		
			
				
				
				
			
		
		
		
	

	
		
			
				
				
				
			
		
		
		
	

		
		
			
				${hibernate.dialect}
				${hibernate.show_sql}
				${hibernate.auto}
				jta
				org.hibernate.search.store.FSDirectoryProvider
				F:/temp/index
			
		
		
			
				com.zyn.ssh.pojo.Student
				com.zyn.ssh.pojo.Teacher
				com.zyn.ssh.pojo.Course
				com.zyn.ssh.pojo.StudentInfo
			
		
	
	
		
		
			
				${hibernate.dialect}
				${hibernate.show_sql}
				${hibernate.auto}
				jta
				org.hibernate.search.store.FSDirectoryProvider
				F:/temp/index
			
		
		
			
				com.zyn.ssh.pojo.Student
				com.zyn.ssh.pojo.Teacher
				com.zyn.ssh.pojo.Course
				com.zyn.ssh.pojo.StudentInfo
			
		
	

	

 测试如下:

 

Java代码  

  1. //第一个dao中注入   

  2. @Resource(name="sessionFactory1")   

  3. public void setSessionFactory(final SessionFactory sessionFactory) {   

  4.         this.sessionFactory = sessionFactory;   

  5. }   

  6.   

  7. //第二个dao中注入   

  8.   

  9. @Resource(name="sessionFactory2")   

  10. public void setSessionFactory(SessionFactory sessionFactory) {   

  11.     this.sessionFactory = sessionFactory ;   

  12. }   

  13.   

  14. @Test  

  15. @Transactional  

  16. public void save(){   

  17.     Student student = new Student("mmmm",20,2008988L);   

  18.     dao1.save(student);   

  19.     dao2.save(student);   

  20. }  

//第一个dao中注入
@Resource(name="sessionFactory1")
public void setSessionFactory(final SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
}

//第二个dao中注入

@Resource(name="sessionFactory2")
public void setSessionFactory(SessionFactory sessionFactory) {
	this.sessionFactory = sessionFactory ;
}

@Test
@Transactional
public void save(){
	Student student = new Student("mmmm",20,2008988L);
	dao1.save(student);
	dao2.save(student);
}