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个回答

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

u012229244
三色球 原来是这样,还是知道的太少了!~~
6 年多之前 回复

不是没有提交,是在测试执行完后,测试框架自动回滚了数据,你如果要验证是否成功保存数据,就在测试用例中判断。

public class PoHeaderServiceTest {

private static PoHeaderService poHeaderService;
private static ItemMasterService itemMasterService;
private static PnGroupColorService pnGroupColorService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
    try {   
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");   
        poHeaderService = (PoHeaderService)ctx.getBean("poHeaderService");
        itemMasterService = (ItemMasterService)ctx.getBean("itemMasterService");
        pnGroupColorService = (PnGroupColorService)ctx.getBean("pnGroupColorService");
    } catch (RuntimeException e) {   
        e.printStackTrace();   
    } 
}

@Test
public void testGetObjectsStringStringObjectArrayStringLinkedHashMapOfStringString() {
    fail("Not yet implemented");
}

}
这样写单元测试试试

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问