javax.persistence.QueryTimeoutException:无法获取JDBC连接Spring Boot Hibernate + JPA

时间:2018-12-11 17:38:25

标签: java spring hibernate spring-boot

我有一个Spring Boot(版本2.0.2版本)应用程序,在SQL Server上配置了hibernate + jpa。

我的应用程序随机抛出此异常:

javax.persistence.QueryTimeoutException:无法获取JDBC连接。

错误应用程序被阻止后,直到我重新启动tomcat。

这是我的application.properties:

#PORT
server.port=8080
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
#spring.jpa.open-in-view=false
endpoints.jmx.unique-names=true
datasource.username=...
datasource.password=...
datasource.url=jdbc:sqlserver://ipaddress:1433;databaseName=dbname
datasource.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=INFO

这是完整的堆栈跟踪:

    Caused by: org.hibernate.QueryTimeoutException: Unable to acquire JDBC Connection
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:66) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:109) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:524) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at sun.reflect.GeneratedMethodAccessor416.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_162]
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:223) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:207) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle.doGetConnection(HibernateJpaDialect.java:391) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:154) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:400) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    ... 112 more
Caused by: java.sql.SQLTimeoutException: Timeout after 30002ms of waiting for a connection.
    at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:233) ~[HikariCP-java6-2.3.13.jar:?]
    at com.zaxxer.hikari.pool.BaseHikariPool.getConnection(BaseHikariPool.java:183) ~[HikariCP-java6-2.3.13.jar:?]
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:110) ~[HikariCP-java6-2.3.13.jar:?]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.NonContextualJdbcConnectionAccess.obtainConnection(NonContextualJdbcConnectionAccess.java:35) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:106) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:136) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:524) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at sun.reflect.GeneratedMethodAccessor416.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_162]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_162]
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:223) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:207) ~[spring-core-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle.doGetConnection(HibernateJpaDialect.java:391) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:154) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:400) ~[spring-orm-5.0.6.RELEASE.jar:5.0.6.RELEASE]
    ... 112 more

这是我的休眠配置类:

public class HibernateConfiguration {

@Value("${datasource.username}")
private String db_username;
@Value("${datasource.password}")
private String db_password;
@Value("${datasource.url}")
private String db_url;
@Value("${datasource.driver}")
private String db_driver;


@Bean
public BeanPostProcessor persistentTransaction() {

    return new PersistenceExceptionTranslationPostProcessor();
}

@Bean
@Primary
public DataSource dataSource() {

    return DataSourceBuilder
            .create()
            .username(db_username)
            .password(db_password)
            .url(db_url)
            .driverClassName(db_driver)
            .build();

}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty("hibernate.jdbc.batch_size", "30");
    properties.setProperty("hibernate.order_inserts", "true");
    properties.setProperty("hibernate.order_updates", "true");
    properties.setProperty("hibernate.connection.release_mode", "ON_CLOSE");
    properties.setProperty("ejb.naming_strategy", "layer.controller.configuration.PhysicalNamingStrategyImpl");
    properties.setProperty("hibernate.search.default.directory_provider", "filesystem");



    return properties;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setDataSource(dataSource());
    factory.setPackagesToScan("layer.domain", "layer.application.security.authentication.entities");
    factory.setJpaProperties(getHibernateProperties());
    factory.afterPropertiesSet();

    return factory;
}


@Bean
public SharedEntityManagerBean entityManager() {
    SharedEntityManagerBean entityManager = new SharedEntityManagerBean();
    entityManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return entityManager;
}


@Bean
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return txManager;
}

@Bean
public ImprovedNamingStrategy namingStrategy() {
    return new ImprovedNamingStrategy();
}

}

谢谢。

0 个答案:

没有答案