简体   繁体   English

休眠5-无法获取当前线程的事务同步会话;

[英]hibernate 5 - Could not obtain transaction-synchronized Session for current thread;

I am able to use sessions if I open a new one, however, I am getting this error if I try to get the current session which I guess doesn't exist. 如果我打开一个新会话,则可以使用会话,但是,如果尝试获取我认为不存在的当前会话,则会出现此错误。

persistence-h2.properties persistence-h2.properties

# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/my-database
jdbc.eventGeneratedId=sa
jdbc.user=root
jdbc.pass=root_password

# hibernate.X
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=validate

# hibernate.search.X
hibernate.search.default.directory_provider = filesystem
hibernate.search.default.indexBase = /data/index/default

# envers.X
envers.audit_table_suffix=_audit_log

HibernateConfig.java HibernateConfig.java

package com.buraktas.spring;

import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-h2.properties" })
@ComponentScan({ "com.buraktas" })
public class HibernateConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "com.buraktas.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());
        return sessionFactory;
    }

    @Bean
    public DataSource dataSource() {
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.pass"));
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager() {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    private final Properties hibernateProperties() {
        final Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));

        hibernateProperties.setProperty("hibernate.show_sql", "true");

        // Envers properties
        hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));

        return hibernateProperties;
    }
}

PersonDaoImp.java PersonDaoImp.java

@Repository
public class PersonDaoImp implements PersonDao {

    @Autowired
    private SessionFactory sessionFactory;

    public Person getPerson(int id) {
        Session currentSession = sessionFactory.getCurrentSession();
        return currentSession.get(Person.class, 1);
    }
}

Here if I use sessionFactory.openSession() instead then I am able to query my database. 在这里,如果我使用sessionFactory.openSession()代替,则可以查询数据库。 But I don't want to create a new session and close it every time. 但是我不想创建一个新的会话并每次都关闭它。 I thought I needed to add current_session_context_class and connection.pool_size properties as well, but that didn't solve the problem. 我以为我也需要添加current_session_context_classconnection.pool_size属性,但这并不能解决问题。 So long story short how I can initialize a connection pool or a session pool? 长话短说,我如何初始化连接池或会话池?

Thanks! 谢谢!

这是一个较晚的响应,但是正如我在评论中提到的,在类级别添加@Transactional可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring和Hibernate-无法获得当前线程的事务同步会话 - Spring and Hibernate - Could not obtain transaction-synchronized Session for current thread Hibernate无法获取当前线程的事务同步会话 - Hibernate Could not obtain transaction-synchronized Session for current thread Hibernate SessionFactory,无法获取当前线程的事务同步会话 - Hibernate SessionFactory and could not obtain transaction-synchronized session for current thread 休眠-无法获取当前线程的事务同步会话 - Hibernate - Could not obtain transaction-synchronized Session for current thread 无法检索预绑定的Hibernate会话 - 无法获取当前线程的事务同步会话 - Could not retrieve pre-bound Hibernate session - Could not obtain transaction-synchronized Session for current thread 迁移到Hibernate 4 + Spring 4.2.2:org.hibernate.HibernateException:无法获取当前线程的事务同步Session - migration to hibernate 4 + spring 4.2.2: org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread HibernateException:无法获取当前线程的事务同步会话 - HibernateException: Could not obtain transaction-synchronized Session for current thread 无法获取当前线程的事务同步会话 - Could not obtain transaction-synchronized Session for current thread Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final-无法获得当前线程的事务同步会话 - Spring 4.3.0.RELEASE + Hibernate 5.2.0.Final - Could not obtain transaction-synchronized Session for current thread org.hibernate.HibernateException:无法获取当前线程的事务同步会话 - org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM