Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread

架个spring4+hibernate4的demo,dao层直接注入的sessionFactory,然后用getCurrentSession方法获取session,然后问题来了,直接报错:

Could not obtain transaction-synchronized Session for current thread

提示无法获取当前线程的事务同步session,略微奇怪,这和事务有什么关系..然后百度一下有人说改成用openSession方法就好了,那我又百度了一下这2个方法的区别:

(1)openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。
(2)getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。

看起来这个getCurrentSession方法的确和事务有点关系.然后我加上事务:

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

然后在dao层加上@Transaction注解,再运行ok了..不过好奇驱使吧,看了一下getCurrentSession的源码(我的demo中用的spring的实现类),关键点:

if (TransactionSynchronizationManager.isSynchronizationActive()) {
            Session session = this.sessionFactory.openSession();
            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.MANUAL);
            }
            SessionHolder sessionHolder = new SessionHolder(session);
            TransactionSynchronizationManager.registerSynchronization(
                    new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
            TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
            sessionHolder.setSynchronizedWithTransaction(true);
            return session;
 }
 else {
            throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
 }

然后点进去看了一下isSynchronizationActive()方法:

public static boolean isSynchronizationActive() {
        return (synchronizations.get() != null);
    }

get方法说明:

Returns the value in the current thread‘s copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue method.

然后再看initialValue的说明:

This implementation simply returns null; if the programmer desires thread-local variables to have an initial value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.

到此问题明了了,补充一点之前配置文件中配了事务,不过是原来那种在配置中根据方法名字来定义事务传播的方式,但是在dao中并没有继承它,故实际上是没有事务的,只有实现.而在spring的事务实现中需要判断当前线程中的事务是否同步,而没有事务的时候,那个判断是否同步的方法会因为get返回初始的null值而返回false,最终导致throw一个Could not obtain transaction-synchronized Session for current thread的异常.

综上:spring4+hibernate4,使用hibernate的api的时候需要配置事务的,如果不配置事务会导致获取当前session抛出异常.

时间: 2024-10-25 23:46:18

Hibernate4中使用getCurrentSession报Could not obtain transaction-synchronized Session for current thread的相关文章

Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) org.hibernate.internal.SessionFactoryImpl.get

SpringMVC中Could not obtain transaction-synchronized Session for current thread的解决方案

首先列出错误信息 org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) at org.hibernate.internal.Session

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

spring与hibernate整合报错 org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134) org.hibernate.internal.S

事务配置不对导致:?Could not obtain transaction-synchronized Session for current thread

Struts has detected an unhandled exception: Messages: ?Could not obtain transaction-synchronized Session for current thread File: org/springframework/orm/hibernate5/SpringSessionContext.java Line number: 132 Stacktraces org.hibernate.HibernateExcepti

SpringMVC4+Hibernate4运行报错Could not obtain transaction-synchronized Session for current thread

查了一下相关资料,整理如下: 原因:Hibernate4 No Session found for current thread原因 解决方法: 1.  在spring 配置文件中加入  程序代码 <tx:annotation-driven transaction-manager="transactionManager"/> 并且在处理业务逻辑的类上采用注解  程序代码 @Service public class CustomerServiceImpl implements

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread spring集成Hibernate sessionfactory.getcurrentSession报错

sessionFactory.getCurrentSession()是要基于事务的,解决方法为在javaconfig配置类使用@EnableTransactionManagement注解  并且配置transactionManager bean. 在报错方法中使用@Transactional注解 原文地址:https://www.cnblogs.com/alexmason236/p/9767091.html

报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="txManager"/> 2.没有在方法上挂注解事务标签

Could not obtain transaction-synchronized Session for current thread

配置session有误:在项目中一般都是自动打开session和关闭session,需要在web.xml中加入如下代码: <filter> <filter-name>SpringOpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class><

weblogic使用过程中的部分报错与解决方案

报错一: vim中 E212:无法打开并写入文件 编写配置文件时,常常忘记切换到root用户,导致文件编辑完毕,敲入:wq 退出保存时,出现 E212:无法打开并写入文件 的错误提示.这是由于在该目录下当前用户没有写权限导致,修改该文件的权限即可 报错二: weblogic11g安装成功后,启动报错Getting boot identity from user 在/home/weblogic/Oracle/Middleware/user_projects/domains/base_domain/