三色球 2013-11-12 00:00
浏览 301
已采纳

spring3+hibernate4中使用spring管理事务,事务不自动提交问题

<?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"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
     http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd ">
     
    <context:property-placeholder location="classpath:hibernate.properties" />
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> 
    </context:component-scan>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${hibernate.driverClassName}"></property>
        <property name="url" value="${hibernate.url}"></property>
        <property name="username" value="${hibernate.name}"></property>
        <property name="password" value="${hibernate.password}"></property>
    </bean>
    
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect
                </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> 
                <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>  
                <prop key="hibernate.jdbc.batch_size">25</prop>  
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 
               
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>
                    com/entity/User.hbm.xml
                </value>

            </list>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">      
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
       <!-- 开启AOP监听 只对当前配置文件有效 -->
     <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>
    
    <!-- 开启注解事务 只对当前配置文件有效 -->
     <tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" proxy-target-class="true"/> 
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED"></tx:method>
        </tx:attributes>
    </tx:advice>
    <aop:config expose-proxy="true" proxy-target-class="true">
        <aop:pointcut expression="within(com.*)" id="allMethod" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />
    </aop:config>
</beans>
package com.test;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.dao.UserDao;
import com.entity.User;
@ContextConfiguration({"classpath:applicationContext.xml"})
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
public class Test {
    @Autowired
    private UserDao userDao;
    @org.junit.Test 
    public void test()
    {
        User user=new User();
        user.setEMail("993126013@qq.com");
        user.setRealname("张三");
        user.setPassword("12333333");
        user.setMobile("15850597175");
        user.setSex("1");
        user.setUsername("zhulljk");        
        int i=userDao.addUser(user);
        System.out.println(userDao.getUsername(user.getUsername()).getId());
    }
}
Hibernate: select MYSEQ.nextval from dual
org.hibernate.internal.SessionFactoryImpl@1de8adb
Hibernate: insert into ZHUT.S_USER (USERNAME, REALNAME, PASSWORD, SEX, TELPHONE, MOBILE, E_MAIL, ID) values (?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: select this_.ID as ID0_0_, this_.USERNAME as USERNAME0_0_, this_.REALNAME as REALNAME0_0_, this_.PASSWORD as PASSWORD0_0_, this_.SEX as SEX0_0_, this_.TELPHONE as TELPHONE0_0_, this_.MOBILE as MOBILE0_0_, this_.E_MAIL as E8_0_0_ from ZHUT.S_USER this_ where this_.USERNAME=?
41
sql已经执行成功,但是事务没有提交,我使用spring来管理事务,不知道哪个地方出现错误,求解惑。。

 

  • 写回答

3条回答 默认 最新

  • iteye_14005 2013-11-13 10:47
    关注

    spring test测试默认会将事务回滚,如果想阻止spring transactional回滚,在test方法上加注解@Rollback(false)即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证