⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactiondaobean.java

📁 精通EJB3源码
💻 JAVA
字号:
package com.foshanshop.ejb3.impl;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.foshanshop.ejb3.OtherDAO;
import com.foshanshop.ejb3.AppException;
import com.foshanshop.ejb3.TransactionDAO;
import com.foshanshop.ejb3.bean.Product;

@Stateless
@Remote (TransactionDAO.class)
public class TransactionDAOBean implements TransactionDAO {
    @PersistenceContext protected EntityManager em;
    @EJB OtherDAO otherejb;
    @Resource SessionContext ejbcontext;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void systemException() {//抛出javax.ejb.EJBTransactionRolledbackException
    	otherejb.systemException();
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void AppException() throws AppException{
    	em.persist(new Product("巴巴运动网", (float)27));
        throw new AppException ("抛出应用例外");//会引起事务回滚
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void setRollback(){
    	em.persist(new Product("手工设置事务回滚", (float)86));
    	ejbcontext.setRollbackOnly();
    }
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void requried_notInTransaction(){
    	otherejb.required();//该方法会为自己开启事务
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void requried_inTransaction(){
    	otherejb.required();//该方法会成为当前事务的一部分
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Product notSupported_runInTransaction(Product product){
    	em.persist(product);
    	em.flush();
    	//下面一行代码执行前,当前事务被挂起
    	Product result = otherejb.notSupported(product.getProductid());//如果事务的隔离级别为Read Committed(避免脏读),该方法的返回结果为null,这是因为该ejb不在当前事务,而当前事务尚未提交,所以获取不到刚插入的记录
    	//上面一行代码执行结束后,原先的事务便会恢复
    	product.setName("事务恢复后的更新");
    	return result;
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void mandatory_inTransaction(){
    	otherejb.mandatory();//该方法会成为当前事务的一部分
    }
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void mandatory_notInTransaction(){
    	otherejb.mandatory();//执行到该方法会抛出EJBTransactionRequiredException例外
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void requirednew_inTransaction(){
    	Product p = new Product("巴巴运动网", (float)56);
    	em.persist(p);
    	otherejb.requirednew();//该方法会为自己开启一个新事务,原有事务被挂起,直到该方法执行结束,原有事务才会恢复执行
    	p.setName("运动用品");
    }
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void requirednew_notInTransaction(){
    	otherejb.requirednew();//该方法会为自己开启一个新事务
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Product support_inTransaction(){
    	Product p = new Product("体育用品", (float)920);
    	em.persist(p);
    	em.flush();
    	Product result = otherejb.support(p.getProductid());//该方法会成为当前事务的一部分
    	p.setName("户外用品");
    	return result;
    }
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public Product support_notInTransaction(){
    	return otherejb.support(1);//因当前没有事务,所以该方法在没有事务的环境下执行
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Product never_inTransaction(int productid){
    	return otherejb.never(productid);//因方法在事务范围内被调用,所以容器会抛出EJBException例外
    }
    
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public Product never_notInTransaction(int productid){
    	return otherejb.never(productid);//该方法在事务范围外调用
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -