我是用s2sh,事务是配置在service层,但我在service层的方法中调用两个dao层的插入方法A方法和B方法,在这两个方法之中故意写一段异常代码测试事务是否回滚,测试时程序是报错了,但A方法不会回滚,不知道是什么原因?请问一下大家是怎样配置声明式事务??我的配置<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.tfl.shop.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
我的包目录如下:
com.tfl.shop.action
com.tfl.shop.common
com.tfl.shop.dao
com.tfl.shop.dao.impl
com.tfl.shop.manage
com.tfl.shop.manage.impl
com.tfl.shop.model
com.tfl.shop.service
com.tfl.shop.vo
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sf" />
</bean>
<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.tfl.shop.service.*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
我的包目录如下:
com.tfl.shop.action
com.tfl.shop.common
com.tfl.shop.dao
com.tfl.shop.dao.impl
com.tfl.shop.manage
com.tfl.shop.manage.impl
com.tfl.shop.model
com.tfl.shop.service
com.tfl.shop.vo
解决方案 »
- 树形菜单的修改子菜单的方法
- java异常处理的应该如何去使用
- 全球知名播放器厂商高薪急寻System operator
- 数据库连接关闭了吗??
- spring配置问题
- html:messages和html:errors的区别
- [小妹求救]公司要求俺着重学习b/s方面的理论知识,请问高手们,对于J2EE这边主要应该注重什么理论呢?顶者有分…
- 散100分,庆祝自己的hibernate和JTA有点入门了!!
- 一个大家同样会遇到的问题!!!
- 载入JAVA小应用程序失败??????JAVA 2 V1.4.2_02谢谢
- 【POI读取EXCEL模板抛错】java.io.IOException: Invalid header signature;
- could not insert报错 SSH实例出错
String admin = testDao.add();
String [] a = null;
//System.out.println(a[0]);
String admin2 = testDao.add2();
if("true".equals(admin) && "true".equals(admin2)){
return "ture";
}
return "false";
在里面继续添加啊
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Throwable"/>
public boolean add()throws Exception{}
这个配置给你参考吧: <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop> -->
</props>
</property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 通知哪个事务类来管理事务 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 设置它的属性 -->
<tx:attributes>
<tx:method name="get*" read-only="true" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="updateStock" no-rollback-for="MyApplicationException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- <aop:config/> 的定义,确保由txAdvice bean定义的事务通知在应用中具体点被执行 -->
<aop:config>
<aop:pointcut id="ServiceMethod" expression="execution(* com.wb.sms.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="ServiceMethod"/>
</aop:config>
把 <tx:method name="*" read-only="true" />放到最后一个试试。。你这个直接放在最前面。。那后面还配置出来干嘛?
<property name="sessionFactory" ref="sf" />除了检查一下这里以外 我看不出你的配置里面哪里有问题了
你的类应该是public修饰的吧、?你配置的是public类切入。。
事物没有起作用 被springmvc的框架包住了
<context:component-scan base-package="com.softfz.dao.impl" />
<context:component-scan base-package="com.softfz.service.impl" />
<!-- 事务管理 -->
<bean id="tranManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="advice" transaction-manager="tranManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="bizMethods"/>
</aop:config>这是我的事务配置
public void deleteMenu(int menuId) throws Exception{
this.menuDao.deleteMenu(menuId);
}这是对应的事务监控方法
<aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>还有这里一定要抛throws Exception??
<aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
这一句是说明事务管理的路径,
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>
rollback-for指明异常类型
<aop:pointcut id="bizMethods" expression="execution(* com.orderfood.biz..*.*(..))"/>
<!-- 定义一个事务拦截器 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice> <!--定义模块的service切入 -->
<aop:config>
<aop:pointcut id="customerPoint"
expression="execution(* com.quanguowuliu.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="customerPoint" />
</aop:config>第二种注解
@Service
@Transactional
标示上就可以了
我是用s2sh,事务是配置在service层,但我在service层的方法中调用两个dao层的插入方法A方法和B方法,在这两个方法之中故意写一段异常代码测试事务是否回滚,测试时程序是报错了,但A方法不会回滚,不知道是什么原因?请问一下大家是怎样配置声明式事务??我的配置XML code <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sf" /> </bean> <aop:config> <aop:pointcut id="bussinessService" expression="execution(public * com.tfl.shop.service.*.*(..))" /> <aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> </tx:attributes> </tx:advice>你设的SERVICE方法中的方法名必须以 add,update,delete 开头命门规则。2。楼上几位也说了,你必须声明抛出哪种异常时,SERVICE事务会回滚。希望对你有用