Spring编程式事务遇到的问题

予怀 2014-11-10 11:18:03
1、项目结构

2、关于spring容器用到的配置文件内容如下
<?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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启注解 -->
<context:annotation-config/>
<!-- 搜索bean组件和切面类 -->
<context:component-scan base-package="my.study.spring.service,my.study.spring.advice">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<!-- 启动AspectJ支持 ;未配置下面的AOP注解功能需要这个配置-->
<aop:aspectj-autoproxy/>

<!-- 增加事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<tx:advice id="noTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="NEVER"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txOperation" expression="execution(* my.study.spring.dao.*.*(..))"/>
<aop:advisor pointcut-ref="txOperation" advice-ref="txAdvice"/>
</aop:config>
<!-- Beans -->
<!-- 开启AOP注解 -->
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- Hibernate 局部事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 业务组件 -->
<!-- <bean id="aspectHello" class="my.study.spring.service.impl.AspectHelloImpl">
</bean> -->
<bean id="newsDao" class="my.study.spring.dao.impl.NewsDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
3、测试代码的核心部分如下<插入两条数据;第一条正确;第二条title为空违反了数据表不为空的约束>
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
NewsDao nd = (NewsDao)ctx.getBean("newsDao");
News n = new News();
n.setId(UUID.randomUUID().toString());
n.setTitle("ok43");
n.setContent("今天很和平");
nd.create(n);
News n2 = new News();
n2.setId(UUID.randomUUID().toString());
//n2.setTitle("");
n2.setContent("今天很和平");
nd.create(n2);
4、控制台输出结果为:<底层数据库插入了第一条数据;第二条数据违反约束所以未插入。>
Caused by: java.sql.BatchUpdateException: Column 'title' cannot be null
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 18 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'title' cannot be null
5、我的问题:
按理说因为增加了Spring事务特性,两条插入的代码也处在同一个事务当中;第二条发生了异常应该一起回滚才对,不应该插入一条数据?各位大侠,请赐教。
...全文
230 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
予怀 2014-11-11
  • 打赏
  • 举报
回复
OK。问题解决了。谢谢。
猎魔人-不纯 2014-11-11
  • 打赏
  • 举报
回复
引用 4 楼 Assassin_Me 的回复:
1、一楼二楼的建议很好。我试了把加在dao层的事务转移到service层语句是没问题了。是不是添加的事务一般要加到service层? 2、正常设置好数据之后,数据的SQL插入语句显示正常,可是不能保存更新到数据库。为什么呢?
事务一般加在业务层代码上,一般就是service,在同一个连接同一个事务下对数据进行插入修改删除出错可以回滚
予怀 2014-11-11
  • 打赏
  • 举报
回复
1、一楼二楼的建议很好。我试了把加在dao层的事务转移到service层语句是没问题了。是不是添加的事务一般要加到service层? 2、正常设置好数据之后,数据的SQL插入语句显示正常,可是不能保存更新到数据库。为什么呢?
Inhibitory 2014-11-10
  • 打赏
  • 举报
回复
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'title' cannot be null
Lucky_fishy 2014-11-10
  • 打赏
  • 举报
回复
你的事务加在DAO上了,每次插入操作都是一个事务
猎魔人-不纯 2014-11-10
  • 打赏
  • 举报
回复
两个事务 事务加在dao上了

67,513

社区成员

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

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