springmvc+hibernate整合事务不回滚

最近心血来潮研究下了springmvc,发现比struts2好用多了,配置也方便,捣鼓了一阵,最后想把hibernate也整进去,结果悲剧就来了,事务就是不回滚,实在没招了,哪位大侠给看下上代码 

springmvc-servlet.xml 
Java代码   收藏代码
  1.   
  2.   
  3.       
  4.     package="com.springmvc" use-default-filters="false">  
  5.         "annotation" expression="org.springframework.stereotype.Controller"/>  
  6.         "annotation" expression="org.springframework.stereotype.Service" />  
  7.       
  8.   
  9.     
  10.         class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">  
  11.         "mediaTypes">  
  12.               
  13.                 "html" value="text/html" />  
  14.                 "json" value="application/json" />  
  15.               
  16.           
  17.         "viewResolvers">  
  18.               
  19.                 class="org.springframework.web.servlet.view.BeanNameViewResolver" />  
  20.                 
  21.                     class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  22.                     "prefix" value="/WEB-INF/views/" />  
  23.                     "suffix" value=".jsp" />  
  24.                   
  25.               
  26.           
  27.         "defaultViews">  
  28.               
  29.                 
  30.                     class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />  
  31.               
  32.           
  33.       
applicationContext-db.xml 
Java代码   收藏代码
  1.   
  2.     package="com.springmvc" />   
  3.       
  4.       
  5.     "mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  6.         "locations" value="classpath:dataSources.properties">  
  7.        
  8.       
  9.     "dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  10.         "driverClassName" value="${driverClassName}" />  
  11.           
  12.         "url" value="${url}" />  
  13.         "username" value="${username}" />  
  14.         "password" value="${password}" />  
  15.         "maxActive" value="${maxActive}" />  
  16.         "maxIdle" value="${maxIdle}" />  
  17.         "maxWait" value="${maxWait}" />  
  18.         "defaultAutoCommit" value="${defaultAutoCommit}" />  
  19.       
  20.       
  21.       
  22.     "sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  23.         "dataSource" ref="dataSource"/>  
  24.           
  25.         "packagesToScan" value="com.springmvc.domain"/>  
  26.         "hibernateProperties">  
  27.               
  28.                 "hibernate.dialect">${hibernate.dialect}  
  29.                 "hibernate.show_sql">true  
  30.                 "hibernate.hbm2ddl.auto">none  
  31.                 "hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider   
  32.                 "hibernate.search.default.transaction.merge_factor">100      
  33.                 "hibernate.search.default.transaction.max_buffered_docs">100   
  34.                 "hibernate.search.autoregister_listeners">true   
  35.                 "hibernate.search.indexing_strategy">manual   
  36.               
  37.           
  38.       
  39.       
  40.     "transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  41.         "sessionFactory" ref="sessionFactory"/>  
  42.       
  43.       
  44.     "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
  45.         "dataSource" ref="dataSource"/>  
  46.       
  47.       
  48.     "transactionManager" proxy-target-class="true"/>  
applicationContext-aop.xml
Java代码   收藏代码
  1. "txAdvice" transaction-manager="transactionManager">  
  2.           
  3.             "update*" propagation="REQUIRED"/>    
  4.             "save*" propagation="REQUIRED"/>    
  5.             "add*" propagation="REQUIRED"/>    
  6.             "create*" propagation="REQUIRED"/>    
  7.             "do*" propagation="REQUIRED"/>    
  8.             "del*" propagation="REQUIRED"/>    
  9.             "remove*" propagation="REQUIRED"/>    
  10.             "get*" read-only="true" />    
  11.             "query*" read-only="true" />    
  12.             "find*" read-only="true" />    
  13.             "*"/>    
  14.           
  15.       
  16.   
  17.     class="true">  
  18.         "execution(public * com.springmvc.service.impl.*ServiceImpl.*(..))" id="allServiceMethod"/>  
  19.         "allServiceMethod"   advice-ref="txAdvice" />  
  20.       
UserServiceImpl
Java代码   收藏代码
  1. Service(value = "userService")  
  2. @Transactional(rollbackFor=Exception.class)  
  3. public class UserServiceImpl implements UserService {  
  4.   
  5.     @Autowired  
  6.     private UserDAO userDAO;  
  7.   
  8.     @Override  
  9.     public void addUser(User user) {  
  10.         userDAO.addUser(user);  
  11.     }  
  12.   
  13. }  
UserDAOImpl 
Java代码   收藏代码
  1. @Repository(value = "userDAO")  
  2. public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {  
  3.   
  4.     private Logger logger = Logger.getLogger(getClass());  
  5.       
  6.     @Override  
  7.     public void addUser(User user) {  
  8.         logger.debug("新增用户");  
  9.         this.getHibernateTemplate().save(user);  
  10.     }  
UserServiceTest
Java代码   收藏代码
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations={"file:webroot/WEB-INF/springmvc-servlet.xml","classpath:applicationContext-*.xml"})  
  3. public class UserServiceTest{  
  4.   
  5.     @Autowired  
  6.     private UserService userService;  
  7.       
  8.     @Test  
  9.     public void testAddUser() {  
  10.           
  11.         User user1 = new User();  
  12.         user1.setCode("A002");  
  13.         user1.setName("haha");  
  14.         userService.addUser(user1);  
  15.           
  16.         User user2 = new User();  
  17.         user2.setCode("A001");  
  18.         user2.setName("haha");  
  19.         userService.addUser(user2);  
  20.           
  21.         User user3 = new User();  
  22.         user3.setCode("A001");//code是唯一的,这里报唯一约束错误,前面两条数据是不是应该插入失败?  
  23.         user3.setName("haha");  
  24.         userService.addUser(user3);  
  25.     }  
  26.   
  27. }  

你可能感兴趣的:(SpringMVC)