首页新闻找找看学习计划

spring4整合hibernate5 事务管理报错 no transaction is in progress 。。。。已跪,求助

0
[待解决问题]

新手,学习ssh整合。

spring4整合hibernate5过程中想实现spring注解方式的事务管理。绞尽脑汁以后依然未解决。。求高人指点。。。。

junit4测试的failure trace如下:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3428)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1395)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1391)
    at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:144)
    at org.springframework.orm.hibernate5.SpringSessionSynchronization.beforeCommit(SpringSessionSynchronization.java:95)
    at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:932)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:744)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:487)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655)
    at com.ssh.service.UserServiceImpl$$EnhancerBySpringCGLIB$$796cde9e.login(<generated>)
    at test.ServiceTest.testLogin(ServiceTest.java:24)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

applicationContext.xml放在了src下,内容如下:

<?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:p="http://www.springframework.org/schema/p"  
         xmlns:aop="http://www.springframework.org/schema/aop"   
         xmlns:context="http://www.springframework.org/schema/context"  
         xmlns:jee="http://www.springframework.org/schema/jee"  
         xmlns:tx="http://www.springframework.org/schema/tx"  
         xsi:schemaLocation="    
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <context:component-scan base-package="com.ssh"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="idleConnectionTestPeriod" value="300"/>
        <property name="maxIdleTime" value="500"/>
        <property name="maxPoolSize" value="5"/>
    </bean>
    
    <!-- 添加sessionFactoryBean ,注意,该类是Spring提供的 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope="prototype">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
         <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
                <!-- 开机自动生成表 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
                <prop key="hibernate.transaction.coordinator_class">jdbc</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.ssh.entity</value>
            </list>
        </property>    
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 使用AspectJ方式配置AOP -->
    <aop:aspectj-autoproxy proxy-target-class="true" />

    <aop:config  proxy-target-class="true">
        <aop:advisor pointcut="execution(* com.ssh.service.*.*(..))" advice-ref="transactionAdvice" /> <!-- 配置哪些类的哪些方法参与事务 --> 
    </aop:config>
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- 配置事务传播特性 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>
 </beans>

service实例(事务管理添加在方法上):

复制代码
package com.ssh.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ssh.dao.UserDao;
import com.ssh.entity.User;
import com.ssh.service.UserService;

@Service("userService")
@Scope("prototype")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userdao;

    @Transactional(propagation=Propagation.REQUIRED,rollbackForClassName="Exception")
    @Override
    public boolean login(User u) {
        System.out.println();
        if(userdao.login(u)){
            return true;
        }else{
            return false;
        }
    }
}
复制代码

求各位大大帮帮忙~~~~~~~~

一只肥腊肠的主页 一只肥腊肠 | 菜鸟二级 | 园豆:202
提问于:2016-12-27 20:32
< >
分享
所有回答(2)
0

你看看这个就知道了

http://www.cnblogs.com/gmq-sh/p/6016590.html

http://blog.csdn.net/lzw190/article/details/18989301

、熙和 | 园豆:1486 (小虾三级) | 2016-12-28 10:56

使用的是Struts,不是SpringMVC。不过还是十分谢谢你~

已经解决了,是hibernate5的在spring下的session工厂类的问题。

实在不好意思,这么久才回复

支持(0) 反对(0) 一只肥腊肠 | 园豆:202 (菜鸟二级) | 2017-01-09 21:25
0

楼主,问题最终是怎么解决的呀

hstonel | 园豆:202 (菜鸟二级) | 2017-04-18 01:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册