如何处理事务

leiru 2008-11-04 05:57:47
我现在想做一个事务,有insert,有update。。
现在想把这些操作做成一个事务,当其中任意一个出错,
我想返回进行事务之前的状态,已经进行的操作也需要还原。
不知道我该如何操作??请高手指教,在线等,问题解决就给分。。
...全文
205 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();
}
}
哈哈,估计好多错误。!
}
第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识。   第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。   第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring框架的三个最重要的框架级接口进行了剖析,并对Bean的生命周期进行讲解。   第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。   第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们还将对Spring容器一些高级主题进行深入的阐述。   第6章:我们从Spring AOP的底层实现技术入手,一步步深入到Spring AOP的内核中,分析它的底层结构和具体实现。   第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。   第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等内容。   第9章:介绍了Spring事务管理的工作机制,通过XML、注解等方式进行事务管理配置,同时还讲解了JTA事务配置知识。   第10章:对实际应用中Spring事务管理各种疑难问题进行透彻的剖析,让读者对Spring事务管理不再有云遮雾罩的感觉。   第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。   第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM框架的混用和DAO层设计的知识。   第13章:本章重点对在Spring中如何使用Quartz进行任务调度进行了讲解,同时还涉及了使用JDK Timer和JDK 5.0执行器的知识。   第14章:介绍Spring 3.0新增的OXM模块,同时对XML技术进行了整体的了解。   第15章:对Spring MVC框架进行详细介绍,对REST风格编程方式进行重点讲解,同时还对Spring 3.0的校验和格式化框架如果和Spring MVC整合进行讲解。   第16章:有别于一般书籍的单元测试内容,本书以当前最具实战的JUnit4+Unitils+ Mockito复合测试框架对如何测试数据库、Web的应用进行了深入的讲解。   第17章:以一个实际的项目为蓝本,带领读者从项目需求分析、项目设计、代码开发、单元测试直到应用部署经历整个实际项目的整体开发过程。

81,116

社区成员

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

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