【问题标题】:Hibernate 4 Spring 4 Could not obtain transaction-synchronized Session for current threadHibernate 4 Spring 4 无法为当前线程获取事务同步会话
【发布时间】:2015-04-04 04:12:56
【问题描述】:
@Repository
public class Init {

    public static void main(String[] args) {

        Init init = new Init();
        init.addUser(init.getSessionFactory());

    }

    private SessionFactory getSessionFactory() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "Spring_Hibernate.xml" });

        SessionFactory sf = (SessionFactory) context.getBean("sessionFactory");

        return sf;
    }

    @Transactional
    private void addUser(SessionFactory sf) {
        Session session = sf.getCurrentSession();

        User user = new User();
        user.setName("123");
        session.save(user);
        session.close();
        sf.close();
    }
}

xml:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.warmmer.bean" />
    <property name="hibernateProperties">
        <!-- <value> hibernate.dialect=org.hibernate.dialect.HSQLDialect </value> -->
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.format_sql">true</prop>

            <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> 
        </props>
    </property>
</bean>


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

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

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

错误: 信息:为 HibernateTransactionManager 使用 Hibernate SessionFactory 的 DataSource [org.apache.commons.dbcp.BasicDataSource@6098b14d] 线程“主”org.hibernate.HibernateException 中的异常:无法获取当前线程的事务同步会话

如果: hibernate.current_session_context_class 设置'线程'

then :save 在没有活动事务的情况下无效

请问我该怎么办?

【问题讨论】:

    标签: java spring hibernate


    【解决方案1】:

    您没有在 spring 上下文中创建“Init”对象,因此 spring 永远不会有机会使用将管理事务的注释围绕方法包装代理

    尝试将您的课程更改为...

    package my.pkg;
    // Imports etc
    
    @Repository
    public class Init {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "Spring_Hibernate.xml" });
            Init init = context.getBean(Init.class);
            init.addUser();
        }
    
        @Transactional
        private void addUser() {
            Session session = sessionFactory.getCurrentSession();
    
            User user = new User();
            user.setName("123");
            session.save(user);
            // session.close(); DON'T NEED THESE!
            // sf.close();
        }
    }
    

    现在您可能需要将以下内容添加到您的 beans 文件中,以便它找到您的存储库...

    <context:component-scan base-package="my.pkg"/>
    

    【讨论】:

    • 不记得@Transactional 是否适用于私有方法,如果不能,请尝试将方法设为公开...
    • 线程“main”中的异常 org.springframework.orm.hibernate4.HibernateSystemException:请求了未知的服务 [org.hibernate.stat.spi.StatisticsImplementor];嵌套异常是 org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.stat.spi.StatisticsImplementor] at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:218)
    • 尝试摆脱 session.close() 和 sf.close() - @Transactional 为你处理这个问题
    猜你喜欢
    • 2015-03-11
    • 2015-04-11
    • 2015-09-22
    • 2014-11-29
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 2017-01-01
    相关资源
    最近更新 更多