ascendlin
2013-08-05 08:42
采纳率: 0%
浏览 357
已采纳

springmvc+hibernate整合事务不回滚,求解

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

springmvc-servlet.xml
[code="java"]mvc:annotation-driven/

<!-- 扫描注解时,将service,DAO层的注解排除,只扫描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>[/code]applicationContext-db.xml 

[code="java"]

<!-- 配置数据源 -->
<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"/>[/code]applicationContext-aop.xml[code="java"]<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"/>  
        <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>[/code]UserServiceImpl[code="java"]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);
}

}[/code]UserDAOImpl
[code="java"]@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);
}[/code]UserServiceTest[code="java"]@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);
}

}[/code]

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • QiangGe2Dai 2013-08-05 11:36
    已采纳

    Service(value = "userService")

    @Transactional(rollbackFor=Exception.class)

    public class UserServiceImpl implements UserService {

    没看到哪里抛出了exception。如果不抛出exception,spring不会rollback

    已采纳该答案
    打赏 评论
  • zm112358 2013-08-05 11:12

    楼主技术用的好混乱啊,既配置了事物边界,又使用了注解式的事物,我建议楼主最好选择 一种,另边界事物配置在接口上而不是实现类中。另楼主介绍下使用的数据库是否支持事物。

    打赏 评论

相关推荐 更多相似问题