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事务特性,两条插入的代码也处在同一个事务当中;第二条发生了异常应该一起回滚才对,不应该插入一条数据?各位大侠,请赐教。