spring4+hibernate4+jotm 分布式事务

1.需要引入jar包:
		
		    com.experlog
		    xapool
		    1.5.0
		
		
		
		    org.ow2.carol
		    carol
		    3.0.10
		
		
		
		    org.ow2.carol
		    carol-iiop-delegate
		    3.0.10
		
		
		
		    org.ow2.carol
		    carol-interceptors
		    1.0.1
		
		
		
		    org.objectweb.joram
		    joram-mom
		    5.0.9
		
		
		
		    org.objectweb.joram
		    joram-shared
		    5.7.0
		
		
		
		    org.ow2.jotm
		    jotm-datasource
		    2.2.3
		
		
		
		    org.ow2.jotm
		    jotm-core
		    2.2.3
		
		
		
		    org.ow2.jotm
		    jotm-standalone
		    2.2.3
		
		
		
		    org.ow2.jotm
		    jotm-jms
		    2.2.3
		
		
		
		    org.ow2.spec.ee
		    ow2-jta-1.1-spec
		    1.0.13
		
		
		
		    org.ow2.spec.ee
		    ow2-connector-1.5-spec
		    1.0.13
		

2、手动创建JotmFactoryBean

import javax.naming.NamingException;
import javax.transaction.SystemException;

import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

@SuppressWarnings("rawtypes")
public class JotmFactoryBean implements FactoryBean, DisposableBean {

	private Current jotmCurrent;

	private Jotm jotm;

	public JotmFactoryBean() throws NamingException {
		// Check for already active JOTM instance.
		this.jotmCurrent = Current.getCurrent();

		// If none found, create new local JOTM instance.
		if (this.jotmCurrent == null) {
			// Only for use within the current Spring context:
			// local, not bound to registry.
			this.jotm = new Jotm(true, false);
			this.jotmCurrent = Current.getCurrent();
		}
	}

	public void setDefaultTimeout(int defaultTimeout) {
		this.jotmCurrent.setDefaultTimeout(defaultTimeout);
		// The following is a JOTM oddity: should be used for demarcation
		// transaction only,
		// but is required here in order to actually get rid of JOTM's default
		// (60 seconds).
		try {
			this.jotmCurrent.setTransactionTimeout(defaultTimeout);
		} catch (SystemException ex) {
			// should never happen
		}
	}

	public Jotm getJotm() {
		return this.jotm;
	}

	public Object getObject() {
		return this.jotmCurrent;
	}

	public Class getObjectType() {
		return this.jotmCurrent.getClass();
	}

	public boolean isSingleton() {
		return true;
	}

	public void destroy() {
		if (this.jotm != null) {
			this.jotm.stop();
		}
	}

}
3、配置文件


	  
	
	
	
		
			
				
				
				
			
		
		
		
	
	
		
			
				
				
				
			
		
		
		
	
	
	
    
          
        
			
				com.entity
			
		
        
            
                org.hibernate.dialect.PostgreSQL81Dialect
                true
   				true
				30
				30
				true
				true
				true
				net.sf.ehcache.hibernate.EhCacheProvider
				org.hibernate.cache.ehcache.EhCacheRegionFactory
				org.springframework.orm.hibernate4.SpringSessionContext
				auto
				filesystem
				/luceneIndexes
				1800
				100
				64
				1000
				100
            
        
    
    
    
    
        
   		
			
				com.entity
			
		
        
            
                org.hibernate.dialect.MySQLDialect
                true
                true
				30
				30
				true
				true
				true
				net.sf.ehcache.hibernate.EhCacheProvider
				org.hibernate.cache.ehcache.EhCacheRegionFactory
				org.springframework.orm.hibernate4.SpringSessionContext
				auto
				filesystem
				/luceneIndexes
				1800
				100
				64
				1000
				100
            
        
    
	
    
    
        
    
    
   	
     
    
        
    
        
    
    
    
        
    
4、实例:

	public void test() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:conf/app-include.xml");
		BsSysUserProvider bsSysUserProvider = ctx.getBean(BsSysUserProvider.class);  
		BsSysRoleProvider bsSysRoleProvider = ctx.getBean(BsSysRoleProvider.class);
		
		bsSysUserProvider.save();
		
		bsSysRoleProvider.save();
	}
        @Resource(name="mysqlHibernateTemplate")
	private HibernateTemplate mysqlHibernateTemplate;
	
	mysqlHibernateTemplate.save(bsSysUser);




你可能感兴趣的:(spring4+hibernate4+jotm 分布式事务)