浏览 4231 次
锁定老帖子 主题:为什么我的Spring的声明事务失效了?
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2006-12-13
public class UserDAO extends BaseDAO { public void insertTestData() { Connection con = null; PreparedStatement ps = null; String sql = null; try { con = getConnection(); sql = "insert into oa_test_1(description) values(?)"; ps = con.prepareStatement(sql); ps.setString(1, "TEST"); ps.execute(); String id = getIdentity(con); if (1 == 1) throw new RuntimeException(); sql = "insert into oa_test_2(description,parentid) values(?,?)"; ps = con.prepareStatement(sql); ps.setString(1, "TEST_2"); setIntParam(ps, new Integer(id), 2); ps.execute(); } catch (SQLException e) { throw new RuntimeException(); } finally { safeCloseStatementOrRs(ps); safeCloseConnection(con); } } } public class BaseDAO { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public Connection getConnection() { try { return dataSource.getConnection(); } catch (Exception e) { throw new RuntimeException(); } } } Service 代码如下: public class UserManagerService { private UserDAO userDao; public void setUserDao(UserDAO userDao) { this.userDao = userDao; } public User userLogon(String username, String password) { userDao.insertTestData(); return null; } } Controller 代码如下: public class PublicController extends MultiActionController { private UserManagerService userService; public void setUserService(UserManagerService userService) { this.userService = userService; } public ModelAndView handleLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException { try { userService.userLogon("james", "12345688"); //WebUtils.setSessionAttribute(request, "user", user); return new ModelAndView(new RedirectView("/cm/index.htm")); } catch (Exception e) { System.out.println("transaction failed........................."); } return null; } } 配置如下: <?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:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> <jee:jndi-lookup id="dataSource" jndi-name="jndi_cm"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"><ref local="dataSource"/></property> </bean> <bean id="userDao" class="com.cm.main.dao.UserDAO"> <property name="dataSource"><ref bean="dataSource"/></property> </bean> <bean id="userServiceTarget" class="com.cm.main.service.UserManagerService"> <property name ="userDao"><ref local="userDao"/></property> </bean> <bean id="userService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target"><ref local="userServiceTarget"/></property> <property name="transactionAttributes"> <props> <prop key="*"> PROPAGATION_REQUIRED,-Exception </prop> </props> </property> </bean> <bean id="publicController" class="com.cm.main.controller.PublicController"> <property name="methodNameResolver"><ref local="cmMethodNameResolver"/></property> <property name="userService"><ref bean="userService"/></property> </bean> </beans> Spring 为2.0版本,appServer 为Weblogic 813 我很疑惑为什么我的声明事务控制失败,操作结果为oa_test_1中插入了一条记录,我明明已经抛出了RuntimeException,事务为什么不回滚,我的Service 和 Dao都没有实现接口方式,而是直接进行实现类的注入,不知道这是否有关系? 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2006-12-13
还是通过<jee:jndi-lookup id="dataSource" jndi-name="jndi_cm"/>得到的DataSource不可以?
|
|
返回顶楼 | |
发表时间:2006-12-13
配置修改成:
<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"><ref bean="transactionManager"/></property> <property name="proxyTargetClass"><value>true</value></property> <property name="transactionAttributes"> <props> <prop key="user*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="userService" parent="txProxyTemplate"> <property name="target"> <bean class="com.cm.main.service.UserManagerService"> <property name="userDao"><ref bean="userDao"/></property> </bean> </property> </bean> 还是不行 修改成 <bean id="matchAllWithPropReq" class="org.springframework.transaction.interceptor.MatchAlwaysTransactionAttributeSource"> <property name="transactionAttribute" value="PROPAGATION_REQUIRED" /> </bean> <bean id="matchAllTxInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributeSource" ref="matchAllWithPropReq" /> </bean> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <idref local="matchAllTxInterceptor" /> </list> </property> <property name="beanNames"> <list> <idref local="userService" /> </list> </property> </bean> <bean id="userService" class="com.cm.main.service.UserManagerService"> <property name="userDao"><ref bean="userDao"/></property> </bean> 还是失败,原来需要DataSourceUtils.getConnection(dataSource);方式取得数据库连接............ |
|
返回顶楼 | |
发表时间:2007-02-07
希望你解决问题了,我也遇到类似的问题不知怎么弄
|
|
返回顶楼 | |
发表时间:2007-08-21
不要自己抛异常,spring不知道那是什么异常,在catch中直接抛出原来的异常就可以了,如果想人为的话,我想也不应直接抛RuntimeException,换成DataAccessException试试。
|
|
返回顶楼 | |