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

📄 bmtdaobean.java

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

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import com.foshanshop.ejb3.BmtDAO;
import com.foshanshop.ejb3.bean.WebSite;

@Stateless
@Remote (BmtDAO.class)
@TransactionManagement(TransactionManagementType.BEAN)
public class BmtDAOBean implements BmtDAO {
    @PersistenceContext protected EntityManager em;
    @Resource SessionContext ejbcontext;
    @Resource UserTransaction ut;
    
    public void commit(WebSite webSite1, WebSite webSite2){
    	try {
			ut.begin();//开始事务
			em.persist(webSite1);
			em.persist(webSite2);
			ut.commit();//提交事务
		} catch (Exception e) {
			e.printStackTrace();
			try {
				ut.rollback();//回滚事务
			} catch (Exception e1) {}
		} 
    }
    
    public void rollback(WebSite webSite1, WebSite webSite2){
    	try {
			ut.begin();
			em.persist(webSite1);
			String babasport = null;
			babasport.toUpperCase();//抛出例外
			em.persist(webSite2);
			ut.commit();
		} catch (Exception e) {
			e.printStackTrace();
			try {ut.rollback();} catch (Exception e1) {}
		} 
    }
    
    public void jndisaveSite(WebSite webSite){
    	try {
    		InitialContext context = new InitialContext();
    		UserTransaction uts = (UserTransaction)context.lookup("java:comp/UserTransaction");
    		uts.begin();
			em.persist(webSite);
			uts.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}     	
    }
    
    public void contextsaveSite(WebSite webSite){
    	try {
    		UserTransaction uts = ejbcontext.getUserTransaction();
    		uts.begin();
			em.persist(webSite);
			uts.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}     	
    }
}

⌨️ 快捷键说明

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