Hibernate 4和Spring 4:无法为当前线程获取事务同步的会话。

3
@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" />

错误: INFO:正在使用Hibernate SessionFactory的数据源[org.apache.commons.dbcp.BasicDataSource @ 6098b14d]进行HibernateTransactionManager 主线程中的异常“org.hibernate.HibernateException:无法为当前线程获取事务同步会话”

如果: hibernate.current_session_context_class设置为'thread'

那么:没有活动事务,save不是有效的操作

请问我该怎么做?

1个回答

2

您没有在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 是否适用于私有方法,如果不适用,请尝试将该方法更改为公共方法... - BretC
主线程中出现异常:org.springframework.orm.hibernate4.HibernateSystemException: 未知的服务请求 [org.hibernate.stat.spi.StatisticsImplementor];嵌套异常是 org.hibernate.service.UnknownServiceException: 未知的服务请求 [org.hibernate.stat.spi.StatisticsImplementor] 在 org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:218) - wmmj23
尝试摆脱session.close()和sf.close() - @Transactional处理这个问题。 - BretC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接