春天& Hibernate SessionFactory性能问题

时间:2017-01-27 08:59:17

标签: spring hibernate spring-boot spring-data sessionfactory

我遇到了Hibernate sessionFactory的性能问题。 它是一个Spring Boot - Hibernate应用程序,其SessionFactory配置如下

@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
    return hemf.getSessionFactory();
}

我也尝试过这个问题Spring Boot - Handle to Hibernate SessionFactory

中描述的所有不同方式

我的DAO看起来像这样

@Autowired
private SessionFactory sessionFactory;


@Transactional
public List<Type> findAll() {
    return sessionFactory.getCurrentSession().createQuery("from Type").list();
}

当并发数据库请求数大于配置的maximumPoolSize(本例中为10)时,应用程序将无响应。

@RequestMapping(value = "/stress-sessionfactory")
public void stressTest(@RequestParam int threadsCount) {
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < threadsCount; i++) {
        final int k = i;
        Runnable runnable
                = () -> {

            List<Type> all = typeDAOHibernate.findAll();
            LOG.info("{}:sessionfactory:{} ", k, all.size());
        };
        Thread t = new Thread(runnable);
        threads.add(t);
    }
    threads.stream().forEach(t -> t.start());
}

您可以在github中找到一个独立的示例。 该示例配置为maximumPoolSize=10。 所以,如果你只是打  http://localhost:8080/stress-sessionfactory?threadsCount=11你会得到我正在谈论的错误。

另一方面,Spring Data存储库可以轻松处理数千个并发请求! (例如http://localhost:8080/stress-jpa?threadsCount=2000

我尝试了不同数据源(Hikari,Tomcat),不同数据库(oracle,h2)和不同hibernate的相同场景 版本(5.011-Final,v4.3.11-Final),我总是得到同样的错误。

Stacktrace

Exception in thread "Thread-51" Exception in thread "Thread-47" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) at com.example.dao.TypeDAOHibernate$$EnhancerBySpringCGLIB$$e6373e2e.findAll(<generated>) at com.example.controller.StressController.lambda$stressTest$0(StressController.java:36) at java.lang.Thread.run(Thread.java:745) Caused by: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1700) at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:48) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:189) at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380) ... 9 more Caused by: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:90) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:112) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getConnectionForTransactionManagement(LogicalConnectionManagedImpl.java:230) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:237) at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214) at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52) at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512) at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45) ... 11 more Caused by: java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30001ms. at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:591) at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:194) at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:146) at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386) at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:87) ... 18 more []

1 个答案:

答案 0 :(得分:0)

我注意到你正在注入sessionFactory但没有使用spring-framework事务功能。 如果你想这样使用,你应该关闭你从dao层的sessionFactory获得的会话。