 |
上一主题
我写的测试代码
<%
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.webs..
|
|
下一主题
1,容器管理的事务:事先在ejb-jar.xml中写了事务类型为Container,方法事务属性为Required,是否就表明在调用这个ben方法的时候,如果没有传递事务语境,那么就新建一个事务,在方..
|
|
|
|
EJB 中的事务回滚问题大家来帮帮忙??
|
2005年03月16日 20:32
|
|
|
标签列表
|
|
我在开发时 UserTransaction transaction=context.getUserTransaction(); try { transaction.begin(); (1) turnToUserID(100,custonerID);//更新数据库成功 (2)turnToOtherUserID(100,customerIDs);//此方法产生异常 transaction.commit(); }catch(Throwable e) { try { transaction.rollback(); }catch(SystemException se){throw new EJBException("");} } 可是turnToUserID的更新还成功并不取消更新这是怎么回事啊 我用的是j2sdkee服务器 我把1,2改为 StringBuffer对象str (1)str.append("aa"); System.out.print(str); (2)if (true){throw new EJBException("");} str.append("bb"); System.out.print(str); 还是能输出aa这是怎么回事啊 怎么不回滚阿 是不是有什么限制还是没用对阿 请高手执教
|
|
|
|
|
|
Re: EJB 中的事务回滚问题大家来帮帮忙?? size=
|
2005年03月16日 20:59
|
|
|
还是让高手们看看所有代码把 package com.atm; import javax.ejb.*; import javax.sql.*; import javax.naming.*; import java.sql.*; import javax.rmi.*; import javax.transaction.*; public class ATMBean implements SessionBean { private Connection conn; private PreparedStatement stat; private String sql; private SessionContext context; private String customerID; private UserTransaction transaction; public void EJBCreate(String customerID)throws CreateException { this.customerID=customerID; try { makeConnection(); } catch(Exception e) { throw new CreateException("连接异常."); } } public void makeConnection()throws Exception { InitialContext contexts=new InitialContext(); DataSource source=(DataSource)contexts.lookup("java:comp/env/jdbc/ATMDB"); conn=source.getConnection(); } public void updateChecking(double d,String customerID)throws Exception { sql="update checking set balance=balance+? where customerID=?"; stat=conn.prepareStatement(sql); stat.setDouble(1,d); stat.setString(2,customerID); int i=stat.executeUpdate(); if (i==-1) { System.out.print(" "+i+" "); throw new Exception("更新checking异常."); } stat.close(); } public void updateSaving(double d,String customerIDs)throws SQLException { sql="update saving set balance=balance+? where customerID=?"; stat=conn.prepareStatement(sql); if (d<0) { throw new SQLException("e"); } stat.setDouble(1,d); stat.setString(2,customerIDs); int i=stat.executeUpdate(); if (i==-1) { throw new SQLException("更新saving异常."); } stat.close(); } public void setSessionContext(SessionContext context) { this.context=context; } public void EJBRemove() { try { conn.close(); } catch(Exception e) {} } public void EJBActivate() { try { makeConnection(); } catch(Exception e) {} } public void EJBPassivate() { try { conn.close(); } catch(Exception e) {} } public void takeFromChecking(double d) { transaction=context.getUserTransaction(); try { transaction.begin(); updateChecking(-d,customerID); updateMachine(-d); transaction.commit(); } catch(Exception e) { try { transaction.rollback(); } catch(SystemException se) { throw new EJBException(se.getMessage()); } } } public void updateMachine(double d)throws Exception { sql="update machine set balance=balance+?"; stat=conn.prepareStatement(sql); stat.setDouble(1,d); int i=stat.executeUpdate(); if (i==-1) { throw new Exception("添加异常."); } stat.close(); } public void takeFromSaving(double d) { transaction=context.getUserTransaction(); try { transaction.begin(); updateSaving(-d,customerID); updateMachine(-d); transaction.commit(); } catch (Exception e) { try { System.out.print("你好."); transaction.rollback(); } catch (SystemException se) { throw new EJBException(se.getMessage()); } } } public void turnFromCheckingToSaving(double d) { if (d<0) { throw new EJBException(""); } transaction=context.getUserTransaction(); try { transaction.begin(); updateChecking(-d,customerID); updateSaving(d,customerID); transaction.commit(); } catch (Throwable e) { try { transaction.rollback(); } catch (SystemException se) { throw new EJBException(se.getMessage()); } } } public void turnFromSavingToChecking(double d) { if (d<0) { throw new EJBException("d"); } transaction=context.getUserTransaction(); try { transaction.begin(); updateChecking(d,customerID); updateSaving(-d,customerID); transaction.commit(); } catch (Exception e) { try { transaction.rollback(); } catch (SystemException se) { throw new EJBException(se.getMessage()); } } } public void turnToUser(double d,String userID)/*********调用的这个方法************/ { if (d<0) { throw new EJBException(""); } UserTransaction transactions=context.getUserTransaction(); try { transactions.begin(); //conn.setAutoCommit(false); updateSaving(d,userID); updateSaving(-d,customerID); System.out.print("no.."); transactions.commit(); } catch (Throwable e) { try { System.out.print("ok.."); transactions.rollback(); System.out.print("ok.."); } catch (Exception se) { System.out.print("ddfdfd"); throw new EJBException(se.getMessage()); } } } public double getBalance() { double d=0.0; transaction=context.getUserTransaction(); try { transaction.begin(); double a=getCheckingBalance(); double b=getSavingBalance(); d=a+b; transaction.commit(); } catch (Exception e) { try { transaction.rollback(); } catch (SystemException se) { throw new EJBException(se.getMessage()); } } return d; } public double getCheckingBalance()throws Exception { sql="select balance from checking where customerID=?"; stat=conn.prepareStatement(sql); stat.setString(1,customerID); ResultSet rs=stat.executeQuery(); if (rs.next()) { return rs.getDouble(1); } else { throw new Exception("aa"); } } public double getSavingBalance()throws Exception { sql="select balance from saving where customerID=?"; stat=conn.prepareStatement(sql); stat.setString(1,customerID); ResultSet rs=stat.executeQuery(); if (rs.next()) { return rs.getDouble(1); } else { throw new Exception("bb"); } } public String getUsername() { String username=null; sql="select username from user where customerID=?"; try { stat=conn.prepareStatement(sql); stat.setString(1,customerID); ResultSet rs=stat.executeQuery(); if (rs.next()) { username=rs.getString(1); } stat.close(); } catch (Exception e) { throw new EJBException(e.getMessage()); } return username; } } //我所调用的是public void turnToUser(double d,String userID)这个方法 到底怎么回事啊 本来EJB很了解但现在对事物真是不敢说了
|
|
|
|
热点TAG:
AOP
cache
缓存
DDD
EJB
集群
设计模式
Hibernate
IOC
JiveJdon
OO
RBAC
Seam
Spring
Struts
anti spam
|