spring文件怎么配置事务怎样修改才能回滚事务,大家帮忙看下. 需要抛出RuntimeException spring的事务才能回滚 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我在DAO里面抛了throw new RuntimeException();但事物还是没回滚.下面是DAO里面的save方法try{ connection = getConnection(); statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); int result = statement.executeUpdate( insertSql ); if( result == 0 ){ id = null; }else{ throw new RuntimeException(); } }catch( Exception e ){ e.printStackTrace(); id = null; } int result = statement.executeUpdate( insertSql ); 这不是插一条数据到数据库吗? 然后我去数据库看了下着条数据存在啊 <!-- 定义事务拦截器bean --> 中加上试试 <property name="target"> <ref bean="xxxDao" /> </property> 你这样是针对一个DAO做申明式事物,我要做的是对所有的DAO做申明式事物.我也试了下加进去了就会出错. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> <!-- 注册事务管理器 --> <bean id="transactionManager" autowire="byName" class="org.springframework.orm.hibernate3.HibernateTransactionManager" /> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*"/> <tx:method name="delete*"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.manager.dao.*.*(..))"/> </aop:config> </beans> <!-- 定义一个BeanPostProcessor Bean --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>*DAO </value> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor </value> </list> </property> </bean> 修改下<!-- 定义一个BeanPostProcessor Bean --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list><value>*Dao</value><!-- 此处可增加其他需要自动创建事务代理的bean--></list></property> <property name="interceptorNames"> <list> <value>transactionInterceptor </value><!-- 此处可增加其他新的Interceptor --> </list> </property> </bean> 还有你所有的DAO 要在XML文件中定义配置 如<!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理--><bean id="xxxxDao" class="pkg.classname"> <property name="objectnamexx">objectxxxx</property></bean> <!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理--> <bean id="xxxxDao" class="pkg.classname"> <property name="objectnamexx">objectxxxx </property> </bean> 请问 <property name="objectnamexx">objectxxxx </property> 这 是做什么用 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" singleton="true"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"> </ref> </property> <!-- 定义业务逻辑处理组件 --> <bean id="xxxDao" class="class.dao/> <propertye name="sessionFactory" ref="sessionFactory"> </bean> <!-- 定义BeanNameAutoProxyCreator --> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定对满足哪些Bean name的Bean自动生成业务代理 --> <property name="beanNames"> <!-- 下面是所有需要自动创建事务代理的Bean--> <list> <value>xxxDao</value> </list> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> 上面的配置 我已经验证了在save方法里面已经切入了事物控制,但就是不回滚事物 如果你的SAVE方法能够把数据持久化到数据库主话SPRING的AOP起到作用了,不能回滚可能是其它原因 楼上的兄弟我没有用hibernate 郁闷 搞了半天原来我没有在save方法里开启事物,connection.setAutoCommit( false ).这样就ok了.不知道是我配错了还是spring也没自动开启事物, 郁闷 我上面没用spring开启的事物,不好意思。spring配置了事物该怎么编码呢? 我在save方法里面transactionManager.getTransaction( new DefaultTransactionDefinition() ).isNewTransaction();结果是false,这说明spring已经在save方法里面切入了事物吧。 个人喜欢使用annotation,配置文件如下:<?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" destroy-method="close"> <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/> <property name="url" value="jdbc:jtds:sqlserver://10.81.64.43:1433/tiannet;charset=gbk"/> <property name="username" value="sa"/> <property name="password" value="sa"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.show.sql=true </value> </property> <property name="mappingLocations"> <list> <value>classpath:spring/hibernate/User.hbm.xml</value> </list> </property> </bean> <!-- 事务管理 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- enable the configuration of transactional behavior based on annotations --> <tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/> <bean id="myTarget" class="spring.hibernate.Commons"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans> java代码如下:package spring.hibernate;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import org.springframework.transaction.annotation.Transactional;public class Commons extends HibernateDaoSupport { /** * 更新某一字段的操作。并使用事务管理。 */ @Transactional public void updateField() { getHibernateTemplate().bulkUpdate("update spring.User set trueName = '管理员4' where userId = 1 "); //有意制造异常 getHibernateTemplate().bulkUpdate("update spring.User2 set trueName = '管理员4' where userId = 1 "); } public static void main(String[] args) { // TODO Auto-generated method stub ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring/hibernate/hibernate.xml"); Commons um = (Commons)appContext.getBean("myTarget"); um.updateField(); }} 此回复为自动发出,仅用于显示而已,并无任何其他特殊作用楼主截止到2008-06-17 20:15:36的汇总数据:发帖数:1结贴数:0结贴率: 0.00%如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html 安装Oracle 11g出现以下问题 如何用JAVA以及dom4j解决XML中重复数据合并的问题 问几道面试题,当时没做出来 设置了<base>标签.提交页面后.css失效.javascript失效 最新EJB学习资料 返回到请求页面的信息该怎么获取和处理? javaMail报错,大家看看哪里出错了呀。 初学JBoss,请问怎么完整地配置一个JBoss呀。谁能给我一个详细的配置资料呀。谢谢了。 TXT文本去重校验 Idea中用tomcat运行maven项目网页不正常,但打包后运行正常 Eclipse中使用ant打包Ejb的疑问? 关于SSH框架的问题,在线等,万分感谢!
try{
connection = getConnection();
statement = connection.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
int result = statement.executeUpdate( insertSql );
if( result == 0 ){
id = null;
}else{
throw new RuntimeException();
}
}catch( Exception e ){
e.printStackTrace();
id = null;
}
<property name="target">
<ref bean="xxxDao" />
</property>
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!-- 注册事务管理器 -->
<bean id="transactionManager" autowire="byName"
class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
<tx:advice id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.manager.dao.*.*(..))"/>
</aop:config>
</beans>
<!-- 定义一个BeanPostProcessor Bean -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*DAO </value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor </value>
</list>
</property>
</bean>
修改下
<!-- 定义一个BeanPostProcessor Bean -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Dao</value>
<!-- 此处可增加其他需要自动创建事务代理的bean-->
</list>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor </value>
<!-- 此处可增加其他新的Interceptor -->
</list>
</property>
</bean> 还有你所有的DAO 要在XML文件中定义配置 如<!--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理-->
<bean id="xxxxDao" class="pkg.classname">
<property name="objectnamexx">objectxxxx</property>
</bean>
<bean id="xxxxDao" class="pkg.classname">
<property name="objectnamexx">objectxxxx </property>
</bean> 请问 <property name="objectnamexx">objectxxxx </property> 这 是做什么用
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
singleton="true">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"> </ref>
</property> <!-- 定义业务逻辑处理组件 -->
<bean id="xxxDao" class="class.dao/>
<propertye name="sessionFactory" ref="sessionFactory">
</bean>
<!-- 定义BeanNameAutoProxyCreator -->
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<!-- 指定对满足哪些Bean name的Bean自动生成业务代理 -->
<property name="beanNames">
<!-- 下面是所有需要自动创建事务代理的Bean-->
<list>
<value>xxxDao</value>
</list>
</property>
<!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器-->
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
配置文件如下:<?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" destroy-method="close">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://10.81.64.43:1433/tiannet;charset=gbk"/>
<property name="username" value="sa"/>
<property name="password" value="sa"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show.sql=true
</value>
</property>
<property name="mappingLocations">
<list>
<value>classpath:spring/hibernate/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true"/>
<bean id="myTarget" class="spring.hibernate.Commons">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
java代码如下:package spring.hibernate;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;
public class Commons extends HibernateDaoSupport {
/**
* 更新某一字段的操作。并使用事务管理。
*/
@Transactional
public void updateField() {
getHibernateTemplate().bulkUpdate("update spring.User set trueName = '管理员4' where userId = 1 ");
//有意制造异常
getHibernateTemplate().bulkUpdate("update spring.User2 set trueName = '管理员4' where userId = 1 ");
} public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("spring/hibernate/hibernate.xml");
Commons um = (Commons)appContext.getBean("myTarget");
um.updateField();
}
}
楼主截止到2008-06-17 20:15:36的汇总数据:
发帖数:1
结贴数:0
结贴率: 0.00%
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html