Spring编程式事务遇到的问题

J2EE 码拜 9年前 (2015-04-06) 903次浏览 0个评论
 

1、项目结构
Spring编程式事务遇到的问题
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事务特性,两条插入的代码也处在同一个事务当中;第二条发生了异常应该一起回滚才对,不应该插入一条数据?各位大侠,请赐教。

Spring编程式事务遇到的问题
30分
两个事务 事务加在dao上了
Spring编程式事务遇到的问题
10分
你的事务加在DAO上了,每次插入操作都是一个事务
Spring编程式事务遇到的问题
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column “”title”” cannot be null
Spring编程式事务遇到的问题
1、一楼二楼的建议很好。我试了把加在dao层的事务转移到service层语句是没问题了。是不是添加的事务一般要加到service层?
2、正常设置好数据之后,数据的SQL插入语句显示正常,可是不能保存更新到数据库。为什么呢?
Spring编程式事务遇到的问题
引用 4 楼 Assassin_Me 的回复:

1、一楼二楼的建议很好。我试了把加在dao层的事务转移到service层语句是没问题了。是不是添加的事务一般要加到service层?
2、正常设置好数据之后,数据的SQL插入语句显示正常,可是不能保存更新到数据库。为什么呢?

事务一般加在业务层代码上,一般就是service,在同一个连接同一个事务下对数据进行插入修改删除出错可以回滚

Spring编程式事务遇到的问题
OK。问题解决了。谢谢。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring编程式事务遇到的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!