JiveJdon Community Forums
在线344人   首页   主题表   培训咨询   标签   精华   查搜   注册    登陆 RSS
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 1 回复 / 1
 发表新帖子   回复该主题贴
yubaojian0616

悄悄话
发表文章: 24
注册时间: 2004年11月27日 14:51
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这是怎么回事啊 怎么不回滚阿 是不是有什么限制还是没用对阿 请高手执教
yubaojian0616

悄悄话
发表文章: 24
注册时间: 2004年11月27日 14:51
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很了解但现在对事物真是不敢说了
这个主题有 1 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
正在读取,请等待...
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
  发贴前查询 标签列表勿重复发表问题

RSS 手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam