请教Hibernate 及 Spring 事务配置问题

lord_logic 2008-11-20 11:32:07
最近在配置HIBERNATE+SPRING事务的时候总是出现问题,想向大家请教一下
具体表现为两个REQUIRED函数连续调用,第一个函数调用成功,第二个函数调用抛出异常,但是第一个函数没有ROLLBACK而是COMMIT了,不符合事物传播的规则
查看日志也是这样,第一个函数在调用完成后就COMMIT了,而没有保持和第二个函数在同一个transaction中
具体配置如下:
请大家无论如何都要帮我下


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="sys"/>
<property name="password" value=""/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Person.hbm.xml</value>
</list>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save" propagation="REQUIRED" />
<tx:method name="delete" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>

<aop:config>
<aop:advisor pointcut="execution(* business.*.*(..))" advice-ref="txAdvice"/>
</aop:config>



<bean id="person" class="business.PersonImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>



package business;

import org.hibernate.SessionFactory;

public interface Person {

public void save(entity.Person person);
public void delete(entity.Person person);

}



import org.springframework.orm.hibernate3.HibernateTemplate;

import entity.Person;


public class PersonBusiness {

public static void main(String[] args) throws Exception {


ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");

Person person = new Person();
p.save(person);
p.delete(person);

}

}



package business;

import org.hibernate.SessionFactory;

public class PersonImpl implements Person {

private SessionFactory sf;

public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}

public void save(entity.Person person) {
sf.getCurrentSession().save(person);
}


public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}

}
...全文
290 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
toss2000 2008-12-01
  • 打赏
  • 举报
回复
顶2楼
然月枕流君 2008-11-30
  • 打赏
  • 举报
回复
呼啦,好久没看到人用标识配事务了,这种才是王道,我喜欢;
我记得楼主这样配置,应该没什么问题,调用的时候是各用各的事务,事务只是session级别的;

顺便问句:
这样的话,可能不抛异常吗?
是代码问题吧!
    public void delete(entity.Person person) {
throw new UnsupportedOperationException();
}

East271536394 2008-11-30
  • 打赏
  • 举报
回复
自己看看
<?xml version="1.0" encoding="UTF-8"?>

<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- 通过hibernate.cfg.xml配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<!-- 引用 配置SessionFactory的ID-->
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- 配置事务的传播性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 哪些方法有 事务的传播性-->
<tx:attributes>
<!-- 以add开始的方法 REQUIRED表示就用事务,没有就开启事务-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="select*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!-- 除了上面的方法外,其余的方法都是只读 -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 哪些类的方法参与了事务 -->
<aop:config>
<!-- execution(* com.east.spring.managerimpl.*.*(..))这个类的所有方法都用事务 -->
<aop:pointcut id="allManagerMethod" expression="execution(* com.east.spring.manager.impl.*.*(..))"/>
<!-- 引用 pointcut 和 advice-->
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
</beans>
NickCheng 2008-11-24
  • 打赏
  • 举报
回复
阅!
gxiaoqiang1987 2008-11-23
  • 打赏
  • 举报
回复
save方法是save方法,delete方法是delete方法,他们不是同一个事务,所以delete出异常,也和save没有关系,假如
public void save(entity.Person person) {
sf.getCurrentSession().save(person);
delete();//假如你在这里调用delete方法,那在执行时他们是同一个事务
}


    ApplicationContext ctx = new FileSystemXmlApplicationContext("bin\\applicationContext.xml");
business.Person p = (business.Person) ctx.getBean("person");

Person person = new Person();
p.save(person);



假如你的代码是这样的,那你执行save方法时,在执行里面的delete()时,抛出异常,delete会回滚,save也会回滚,因为这时他们是同一个事务,
hl_ghost 2008-11-20
  • 打赏
  • 举报
回复
   程序没有问题,是你的理解有问题,save和delete是两个事务,不是一个!
你把他们俩方到一个方法里
testTran(entity.Person person){
p.save(person);
p.delete(person);
}
<tx:attributes>
<tx:method name="testTran" propagation="REQUIRED" />
</tx:attributes>
这样就可以了。

fosjos 2008-11-20
  • 打赏
  • 举报
回复
很多人在函数里写commit,那么spring的事务管理就无效了
yanqing7026 2008-11-20
  • 打赏
  • 举报
回复
顶3楼的
不建议在DAO层做事务处理,可以在逻辑层处理事务
zx273064010 2008-11-20
  • 打赏
  • 举报
回复
学习
zou_wei_forever 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hl_ghost 的回复:]
   程序没有问题,是你的理解有问题,save和delete是两个事务,不是一个!
你把他们俩方到一个方法里
testTran(entity.Person person){
p.save(person);
p.delete(person);
}
<tx:attributes>
<tx:method name="testTran" propagation="REQUIRED" />
</tx:attributes>
这样就可以了。
[/Quote]
楼上说得对,不建议在每个方法上声明事务
logo 设计不会,就这样先凑合了写道 Fuseblog预备开发带在线SHOP功能的个人博客系统.仿照Appfuse 提供一个完全自动化的ant构建脚本.在线SHOP的数据库建模图例已经 在博客中贴出.感兴趣的和我一起搭建,嘻嘻 文档博客地址: http://forum.sinomagazine.com 开发环境: Jcreator-4.5 JDK-1.5 Ant-1.6.1 MYSQL-5 Tomcat-5.5 Spring-2.5.6 Ibatis-2.3.4 问题事项: 1.在windows系统中,命令行中执行ant命令时,当指定的 构建脚本文件中包含中文字符,而构建脚本文件的编码是UTF-8时将会 Invalid byte 1 of 1-byte UTF-8 sequence. 的错误.这个问题尚未 知解决,故先采用GBK的编码. 2.在Jcreator中创建的项目,不支持UTF-8编码,缺省GBK. 以下先列出初步的的ant构建文件的代码: build.xml 问题,并保证构建成功。 二、工具篇: 持续集成 ... by lixw 2008-09-09 回复 (3) 网络相册开发(6)——ant 自动 ... 在工程目录下创建libs/hibernate/ ,放入 hibernate-tools-3.2.0.ga.jar jtidy-4aug2000r7-dev.jar build.properties project.name=sw project.version=1.0 basedir=. build.dir =${basedir}/build web.dir = ${basedir}/WebR ... by vyyv 2009-03-12 回复 (1) ant配置文件实例详解 build.xml 代码 xml version="1.0" encoding="UTF-8"?> 问题。 Antenna介绍 Ant ... by pandonix 2007-08-28 回复 (9) 用Ant 来编译打包您的Anroid应用 ... 通过 activityCreator.py --out myproject your.package.name.ActivityName 命令可以生成一个project ,生成project下面有个bulid.xml 文件,那个东西就可以帮你打包生成了。 来看下ant是怎么做的。 Hibernatet等ORM的重要区别之一) l 支持基于Object Graph进行自动查询调优 ... by kyo100900 2009-04-07 回复 (7) jspx 一个来自于埃及的java web ... 怎么又冒出一个web框架? 看看现存的Java web应用程序框架,无一例外的要求花费大量的时间与精力后才可投入生产。对于大多数开发人员来说想要的东西很简单:友好,易于学习并且还具有生产力。因此我们开发了JSPX。 JSPX的主要目标就是打造成为一个“友好的开发者”框架。因为JSPX只基于标准的HTML标签和简单的Java POJO: 1. JSP ... by kyo100900 2009-01-07 回复 (42) NetBeans Struts2 插件更新 NetBeans Struts 2 插件(http://nbstruts2support.dev.java.net)很长时间没有更新了,我对原插件进行了少量修改,并在 NetBeans 6.7 上测试通过,其中修改包括:1.包含了最新的 Struts 2.1.6 库文件。2.更新至最新的 web framework API。3.重新设计了配置面板,支持更加灵活的最初配置。4.少量针对的 ... by gml520 2009-08-27 回复 (4) 新版本的PrettyTools发布,支持 ... PrettyFaces: EL API访问PrettyContext 支持JSF 1.1 增强了错误页面和servlet重定向 PrettyFaces是一个JSF1.2和JSF2.0的扩展,用来创建便于书签收藏、漂亮的网址。 PrettyFaces优雅的解决了这个问题,包括诸如功能:网页装载行动,无缝的跟faces的导航整合,动态视图的ID分配和管理参数分析,无需配置,兼容其他JSF框架。P ... by zly06 2009-09-09 回复 (0) 相关博客 ant模板 < ? xml version = "1.0" ?> < project name= "tax-calculator" default= "package" > < property name= "src.dir" location= "src" / > ... by article2008 2008-07-30 回复 (0) 代码备份build.xml

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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