发帖    主题    评论    推荐    标签    作者    订阅    查搜    注册   登陆   关注
 
面向对象 设计模式 领域驱动设计 企业架构 框架 开发教程 微服务 大数据 扩展性 并发编程 事件驱动 分布式 SOA

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这是怎么回事啊 怎么不回滚阿 是不是有什么限制还是没用对阿 请高手执教

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很了解但现在对事物真是不敢说了

赞助商链接

赞助商链接

返回顶部

移动版 关于本站 使用帮助 联系管理员 最佳分辨率1366x768
OpenSource JIVEJDON Powered by JdonFramework Code © 2002-20 jdon.com