如何处理事务

leiru 2008-11-04 05:57:47
我现在想做一个事务,有insert,有update。。
现在想把这些操作做成一个事务,当其中任意一个出错,
我想返回进行事务之前的状态,已经进行的操作也需要还原。
不知道我该如何操作??请高手指教,在线等,问题解决就给分。。
...全文
181 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
startym 2008-11-05
  • 打赏
  • 举报
回复
没错是jdbc!
leiru 2008-11-05
  • 打赏
  • 举报
回复
DBManager dbManager = new DBManager();
try{
dbManager.open("DataSource");
dbManager.beginTransaction();
//插入记录
facade2.update(dto2);
businessinfoAction.updateSalesChance(dbManager,dto);
deliverAction.update(dbManager,dto2);
swfLogAction.update(dbManager,swfLogDto);
command.executeCommand();
SwfLogDto swfLogDto = getSwfLogDto(dto,dto.getFlowid());
workFlowDto = helper.viewToDto(user, swfLogDto) ;
while (it.hasNext()){
SwfLogDto swfDto=(SwfLogDto) it.next();
swfDto.setHandlerCode(dto.getUnderwritecode());
swfDto.setHandlerName(dto.getUnderwritecode());
bLCc_swflogFacade.update(swfDto);
}
dbManager.commitTransaction();
}catch(Exception exception){
dbManager.rollbackTransaction();
throw exception;
}finally{
dbManager.close();
}


我是这么写的,可以吗???
事务里面是一些自己写的update,insert方法。。。
2:另外这个方法facade2.update(dto2);也是用上面的处理事务的方法写的。。
public void update(Crm_deliverDto crm_deliverDto)
throws Exception{
DBManager dbManager = new DBManager();
BLCrm_deliverAction blCrm_deliverAction = new BLCrm_deliverAction();
try{
dbManager.open("platformDataSource");
dbManager.beginTransaction();
//更新记录
blCrm_deliverAction.update(dbManager,crm_deliverDto);
dbManager.commitTransaction();
}catch(Exception exception){
dbManager.rollbackTransaction();
throw exception;
}finally{
dbManager.close();
}
}
我的问题是这种处理2次的事务对上面的程序有影响吗??
谁知道回答一下啊,谢谢,急用~!
leiru 2008-11-05
  • 打赏
  • 举报
回复
我的处理中包括一个数据的查询,事务处理是否可以包含查询记录结果集的操作呢
command.executeCommand();
SwfLogDto swfLogDto = getSwfLogDto(dto,dto.getFlowid());
workFlowDto = helper.viewToDto(user, swfLogDto) ;
像上面的方法是一些查询的操作。可以直接和update的操作放在一起吗?
hl_ghost 2008-11-05
  • 打赏
  • 举报
回复
首先我不知道你的那个DBManager怎么写的,不过jdbc的事务是基于connection的,如果并发时,你那个两事务用的不是同一个connection应该不会有问题
cy729215495 2008-11-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hl_ghost 的回复:]
估计你还没有用到spring的事务,所以如果你是用jdbc那么就手工写吧。

class PersonDAO{
private Connection connection;
public insert(Person person) throws Exception{
PreparedStatemt pt=getConnection().preparedStatement("insert into .........");
..................................................
}
public update (Person perosn) throws Exception{

[/Quote]
一针见血!
leiru 2008-11-04
  • 打赏
  • 举报
回复
我的这中事务处理是属于那种情况的啊??
是jdbc吗?
leiru 2008-11-04
  • 打赏
  • 举报
回复
DBManager dbManager = new DBManager();
try{
dbManager.open("DataSource");
dbManager.beginTransaction();
//插入记录
facade2.update(dto2);
businessinfoAction.updateSalesChance(dbManager,dto);
deliverAction.update(dbManager,dto2);
swfLogAction.update(dbManager,swfLogDto);
command.executeCommand();
SwfLogDto swfLogDto = getSwfLogDto(dto,dto.getFlowid());
workFlowDto = helper.viewToDto(user, swfLogDto) ;
while (it.hasNext()){
SwfLogDto swfDto=(SwfLogDto) it.next();
swfDto.setHandlerCode(dto.getUnderwritecode());
swfDto.setHandlerName(dto.getUnderwritecode());
bLCc_swflogFacade.update(swfDto);
}
dbManager.commitTransaction();
}catch(Exception exception){
dbManager.rollbackTransaction();
throw exception;
}finally{
dbManager.close();
}


我是这么写的,可以吗???
事务里面是一些自己写的update,insert方法。。。
2:另外这个方法facade2.update(dto2);也是用上面的处理事务的方法写的。。
public void update(Crm_deliverDto crm_deliverDto)
throws Exception{
DBManager dbManager = new DBManager();
BLCrm_deliverAction blCrm_deliverAction = new BLCrm_deliverAction();
try{
dbManager.open("platformDataSource");
dbManager.beginTransaction();
//更新记录
blCrm_deliverAction.update(dbManager,crm_deliverDto);
dbManager.commitTransaction();
}catch(Exception exception){
dbManager.rollbackTransaction();
throw exception;
}finally{
dbManager.close();
}
}
我的问题是这种处理2次的事务对上面的程序有影响吗??
hengzhan 2008-11-04
  • 打赏
  • 举报
回复
事务就是一系列操作 只要有一个失败就会回滚,只有全成功了才会提交,

会Spring了没有,如果用了事情就方便了 ,把你想做的一系列操作的方法切一下就好了,但是注意,在dao中不要自己关闭连接,让spring来做

要自己做,这个问题还是有点麻烦的

核心思想应该就是一个connection连接,
try{
connection.setAutoCommit(false);
....

完成操作
connection.commit();

}catch(... ){
connection.rollback();
}

记住是一个连接,spring是用aop完成这个过程的,只还过它因为要通用,所以考虑的问题比较多,

但是用起来还是很方便的

你想用spring去看一下它随源码带的reference.paf吧,内容很好的!
hl_ghost 2008-11-04
  • 打赏
  • 举报
回复
估计你还没有用到spring的事务,所以如果你是用jdbc那么就手工写吧。

class PersonDAO{
private Connection connection;
public insert(Person person) throws Exception{
PreparedStatemt pt=getConnection().preparedStatement("insert into .........");
..................................................
}
public update (Person perosn) throws Exception{
getConnection().preparedStatement("update person set ..........");
..........................................................
}
setter,getter方法。
}
class DogDAO{
同上。。。。。。。。。。。。。。。。。。。。。。。。

}
class ServiceForDogAndPerson{
private Connection conneciton;
private PersonDAO personDAO;
private DogDAO dogDAO;
public transaction(){

connection.setAutoCommit(false);
try{
dogDAO.setConnection(connection);
personDAO.setConnection(connection);
dogDAO.insert(...);
personDAO.update(.....);
connection.commit();
}catch(Exception e){
connection.rollback();
}finally{
connection.close();
}
}
哈哈,估计好多错误。!
}

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧