欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过JNDI获得一个用户事务  

程序员文章站 2022-05-24 08:21:35
...
public class Trans {
    private static Log log = LogFactory.getLog(Trans.class);
    static {
        log.debug("life Trans INITIAL_CONTEXT_FACTORY:"
                + Env.TRANSACTION_INITIAL_CONTEXT_FACTORY);
        log.debug("life Trans PROVIDER_URL:" + Env.TRANSACTION_PROVIDER_URL);
        log.debug("life Trans SECURITY_PRINCIPAL:" + Env.PRINCIPAL);
        log.debug("life Trans SECURITY_CREDENTIALS:" + Env.CREDENTIALS);

    }

    /**
     * 通过JNDI获得一个用户事务句柄
     * Last update 
     * 	date: 2005-05-11
     * 	author: Zhangjr
     * 
     * @return UserTransaction
     */
    public static UserTransaction getUserTransaction() throws NamingException,SystemException {
        UserTransaction trans = null;
        Context ctx = new InitialContext();
        trans = (UserTransaction) ctx.lookup("java:comp/UserTransaction");
        if (trans != null){
            trans.setTransactionTimeout(60*20); // 设置事务超时间为25分钟
            return new UserTransactionWrapper(trans);
        }else{
            return null;
        }
    }