xapool的框架配置

package juan.orm.datasource.impl;

import java.util.Properties;

import javax.sql.DataSource;

import juan.exception.JRuntimeException;
import juan.orm.datasource.IDataSource;

import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.objectweb.jotm.Current;

public class XapoolDataSourceImpl implements IDataSource {
	private String name=null;
	
	public DataSource getDataSource() {
		Properties prop = new Properties();
        try {
            prop.load(ClassLoader.getSystemResourceAsStream("xapool.properties"));
        } catch (Exception e) {
            System.err.println("problem to load properties.");
            e.printStackTrace();
        }

        String login = prop.getProperty("login");
        String password = prop.getProperty("password");
        String url = prop.getProperty("url");
        String driver = prop.getProperty("driver");
        String maxActive = prop.getProperty("maxActive");
        String maxIdle = prop.getProperty("maxIdle");

        StandardXADataSource xads = new StandardXADataSource();
        StandardXAPoolDataSource spds = new StandardXAPoolDataSource();
        try {
        	xads.setUrl(url);
        	xads.setDriverName(driver);
        	xads.setUser(login);
        	xads.setPassword(password);
        	xads.setTransactionManager(Current.getCurrent());
        	xads.setDebug(true);
	
	        // second, we create the pool of connection with the previous object
	        spds.setMaxSize(Integer.parseInt(maxActive));
	        spds.setMinSize(Integer.parseInt(maxIdle));
	        spds.setUser(login);
	        spds.setPassword(password);
	        spds.setDataSourceName(getName());
	        spds.setDataSource(xads);
	        spds.setTransactionManager(Current.getCurrent());
	        spds.setDebug(true);
	        
		} catch (Exception e) {
			e.printStackTrace();
	        throw new JRuntimeException(e);
		}
		return spds;
	}

	
	public String getName() {
		return name;
	}
	
	
	public void setName(String name) {
		this.name=name;		
	}
}

 

 

package juan.transaction;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import juan.exception.JRuntimeException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.objectweb.jotm.Jotm;
import org.objectweb.transaction.jta.TMService;

/**
 * 
 * @author liuth
 * @version 1.0
 * Created on 2003-2-20
 */
public class JOTMTransaction implements ITransaction {
   private static final Log logger = LogFactory.getLog(JOTMTransaction.class);
   private UserTransaction tx;
   private TMService jotm = null;

   public void initial(){
      try {
    	  if(jotm==null)
    		  jotm = new Jotm(true, false);
    	  if(tx==null)
    		  tx = jotm.getUserTransaction();
      } catch (Exception ne) {
         logger.error(ne);
         throw new JRuntimeException(ne);
      }
   }
   
   public void initial(String jndi){
      try {
    	  if(jotm==null)
    		  jotm = new Jotm(true, false);
    	  if(tx==null)
    		  tx = jotm.getUserTransaction();
      } catch (Exception ne) {
         logger.error(ne);
         throw new JRuntimeException(ne);
      }
   }
   
   public void begin() {
      try {
         tx.begin();
      } catch (SystemException se) {
         logger.error(se);
         throw new JRuntimeException(se);
      } catch (NotSupportedException nse) {
         logger.error(nse);
         throw new JRuntimeException(nse);
      }
   }

   public void commit() {
      try {
         tx.commit();        
      } catch (SystemException se) {
         logger.error(se);
         throw new JRuntimeException(se);
      } catch (HeuristicRollbackException hre) {
         logger.error(hre);
         throw new JRuntimeException(hre);
      } catch (HeuristicMixedException hme) {
         logger.error(hme);
         throw new JRuntimeException(hme);
      } catch (RollbackException re) {
         logger.error(re);
         throw new JRuntimeException(re);
      }finally{
    	  close();
      }
   }

   public void rollback() {
      try {
         tx.rollback();
      } catch (SystemException se) {
         logger.error(se);
         throw new JRuntimeException(se);
      }finally{
    	  close();
      }
   } 
   
   private void close(){
	   if(jotm!=null){
		   jotm.stop();
	   }
   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring+iBatis+JOTM实现JTA事务: 如何处理跨库事物:spring + jtom 的jta事务是个很好的选择. 这个源码示例非常不错,包括所有的源码和jar包,下载后eclipse 或 myeclipse 导入就能用。 里面有详细的说明和注释,欢迎下载传播。有问题请在评价中留言,我会及时回复的。 <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/> <!-- JTA事务管理器 --> <bean id="myJtaManager" class="org.springframework.transaction.jta.JtaTransactionManager"> <property name="userTransaction"> <ref local="jotm"/> </property> </bean> <!-- 数据源A --> <bean id="dataSourceA" 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="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> </bean> </property> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 数据源B --> <bean id="dataSourceB" 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="${jdbc2.driver}"/> <property name="url" value="${jdbc2.url}"/> </bean> </property> <property name="user" value="${jdbc2.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 事务切面配置 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* *..servi1ce*..*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> <!-- 通知配置 --> <tx:advice id="txAdvice" transaction-manager="myJtaManager"> <tx:attributes> <tx:method name="delete*" rollback-for="Exception"/> <tx:method name="save*" rollback-for="Exception"/> <tx:method name="update*" rollback-for="Exception"/> <tx:method name="*" read-only="true" rollback-for="Exception"/> </tx:attributes> </tx:advice ...... ...... ......

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值