欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springmvc+hibernate整合事务不回滚,谁来拯救我

程序员文章站 2022-03-30 19:56:58
...
最近心血来潮研究下了springmvc,发现比struts2好用多了,配置也方便,捣鼓了一阵,最后想把hibernate也整进去,结果悲剧就来了,事务就是不回滚,实在没招了,哪位大侠给看下,上代码

springmvc-servlet.xml
<?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:context="http://www.springframework.org/schema/context"
	default-autowire="byName" 
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 启动注解扫描 -->
	<mvc:annotation-driven/>

	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
	
	<!-- 扫描Controller注解 -->
	<context:component-scan base-package="com.springmvc" use-default-filters="false">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	</context:component-scan>


	<!-- 配置视图 -->
	<bean
		class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
		<property name="mediaTypes">
			<map>
				<entry key="html" value="text/html" />
				<entry key="json" value="application/json" />
			</map>
		</property>
		<property name="viewResolvers">
			<list>
				<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
				<bean
					class="org.springframework.web.servlet.view.InternalResourceViewResolver">
					<property name="prefix" value="/WEB-INF/views/" />
					<property name="suffix" value=".jsp" />
				</bean>
			</list>
		</property>
		<property name="defaultViews">
			<list>
				<bean
					class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
			</list>
		</property>
	</bean>

</beans>

applicationContext-db.xml
<?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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
	default-autowire="byName">
	
	<!-- 配置数据源 -->
	<bean id = "mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:dataSources.properties"></property>
	</bean> 
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driverClassName}" />
		
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<property name="maxActive" value="${maxActive}" />
		<property name="maxIdle" value="${maxIdle}" />
		<property name="maxWait" value="${maxWait}" />
		<property name="defaultAutoCommit" value="${defaultAutoCommit}" />
	</bean>
	
	<!-- 配置sessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- 扫描的domain包路径 -->
		<property name="packagesToScan" value="com.springmvc.domain"/>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
				<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop> 
				<prop key="hibernate.search.default.transaction.merge_factor">100</prop>	
				<prop key="hibernate.search.default.transaction.max_buffered_docs">100</prop> 
				<prop key="hibernate.search.autoregister_listeners">true</prop> 
				<prop key="hibernate.search.indexing_strategy">manual</prop> 
			</props>
		</property>
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
	
</beans>

applicationContext-aop.xml
<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context 
			http://www.springframework.org/schema/context/spring-context-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/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	default-autowire="byName">
	
	<context:annotation-config />
	<context:component-scan base-package="com.springmvc"> 
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan> 
	
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="update*" propagation="REQUIRED"/>  
            <tx:method name="save*" propagation="REQUIRED"/>  
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>  
            <tx:method name="create*" propagation="REQUIRED"/>  
            <tx:method name="do*" propagation="REQUIRED"/>  
            <tx:method name="del*" propagation="REQUIRED"/>  
            <tx:method name="remove*" propagation="REQUIRED"/>  
	        <tx:method name="get*" read-only="true" />  
	        <tx:method name="query*" read-only="true" />  
	        <tx:method name="find*" read-only="true" />  
	        <tx:method name="*"/>  
		</tx:attributes>
	</tx:advice>

	<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(public * com.springmvc.service.impl.*ServiceImpl.*(..))" id="allServiceMethod"/>
		<aop:advisor pointcut-ref="allServiceMethod"   advice-ref="txAdvice" />
	</aop:config>

</beans>


UserServiceImpl
@Service(value = "userService")
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDAO userDAO;

	@Override
	public void addUser(User user) {
			userDAO.addUser(user);
	}

}

UserDAOImpl
@Repository(value = "userDAO")
public class UserDAOImpl extends HibernateDaoSupport implements UserDAO {

	private Logger logger = Logger.getLogger(getClass());
	
	@Override
	public void addUser(User user) {
		logger.debug("新增用户");
		this.getHibernateTemplate().save(user);
	}

UserServiceTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:webroot/WEB-INF/springmvc-servlet.xml","classpath:applicationContext-*.xml"})
public class UserServiceTest{

	@Autowired
	private UserService userService;
	
	@Test
	public void testAddUser() {
		
		User user1 = new User();
		user1.setCode("A002");
		user1.setName("haha");
		userService.addUser(user1);
		
		User user2 = new User();
		user2.setCode("A001");
		user2.setName("haha");
		userService.addUser(user2);
		
		User user3 = new User();
		user3.setCode("A001");//code是唯一的,这里报唯一约束错误,前面两条数据是不是应该插入失败?
		user3.setName("haha");
		userService.addUser(user3);
	}

}

就在这里找不到问题了,use3前面写个更新也不行,user2还是会被更新,网上说扫描controller和service的时候分开,我也分开了,难道是DAO有问题还是怎么的,还是说配置有问题,望来位高人指点一二springmvc+hibernate整合事务不回滚,谁来拯救我
            
    
    博客分类: springmvc