用spring3+hibernate配置注解式事务,测试的时候抛出异常总是不回滚。
[size=medium][color=red]spring主配置文件hibernate.xml[/color][/size]
<?xml version="1.0" encoding="UTF-8"?>
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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">
<!--连接关闭时默认将所有未提交的操作回滚。Default: false -->
<property name="autoCommitOnClose" value="true" />
<property name="initialPoolSize" value="20" />
<property name="minPoolSize" value="20" />
<property name="maxPoolSize" value="50" />
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<!--最大空闲时间,240秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<!-- 每240秒检查连接池中所有空闲连接,默认值:0 -->
<!--定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 测试的表必须在初始数据源的时候就存在。Default: null -->
<!--c3p0将建一张名为Test的空表,并使用其自带的查询语句进行测试。如果定义了这个参数那么 属性preferredTestQuery将被忽略。你不能在这张Test表上进行任何操作,它将只供c3p0测试 使用。Default: null <property name="automaticTestTable" value="TEST"
/> -->
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
<!--如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
<!--因性能消耗大请只在需要的时候使用它。如果设为true那么在每个connection提交的 时候都将校验其有效性。建议使用idleConnectionTestPeriod或automaticTestTable 等方法来提升连接测试的性能。Default: false -->
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations" >
<list>
<value>classpath*:/com/qymgr/entity/hbm/*.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.query.substitutions=true 1, false 0
hibernate.jdbc.batch_size=20
hibernate.c3p0.max_statements=0
</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明式事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 异常 -->
<bean id="exceptionResolver" class="com.qymgr.core.exception.ExceptionHandler"/>
[size=medium][color=red]测试的DemoDao接口:DemoDao.java[/color][/size]
package com.qymgr.sysmgr.dao;
import java.util.List;
import com.qymgr.entity.Demo;
public interface DemoDao{
List findAll();
void save(Demo demo);
}
[size=medium][color=red]测试的实现类:DemoDaoImpl.java[/color][/size]
package com.qymgr.sysmgr.dao.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.qymgr.entity.Demo;
import com.qymgr.sysmgr.dao.DemoDao;
@Repository
@Transactional
public class DemoDaoImpl extends HibernateDaoSupport implements DemoDao{
// @Resource HibernateTemplate hibernateTemplate;
public List findAll(){
return getHibernateTemplate().find("from Demo");
}
@Transactional
public void save(Demo demo){
getHibernateTemplate().save(demo);
throw new RuntimeException();
}
}
抛出异常了,但是每回数据都成功插入了数据库,求大神指点