2016-12-31 89 views
1

5-6個請求後我收到錯誤。春季休眠連接泄漏

org.springframework.dao.DataAccessResourceFailureException 
Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection 

使用下面的代碼完美地工作,除了它在幾個請求後耗盡連接池。

我是Spring框架的新手,並使用在線示例組合了所有這些。我嘗試了幾個變體,都失敗了。任何幫助,將不勝感激。謝謝。

application.yml

spring: 
    datasource: 
     type: com.zaxxer.hikari.HikariDataSource 
     dataSourceClassName: com.mysql.jdbc.jdbc2.optional.MysqlDataSource 
     jdbcUrl: jdbc:mysql://localhost:3306/db_name?autoReconnect=true&useSSL=false&useUnicode=true&characterEncoding=utf8 
     catalog: db_name 
     username: myusername 
     password: mypassword 
     testOnBorrow: true 
     validationQuery: SELECT 1 
     testWhileIdle: true 
     timeBetweenEvictionRunsMillis: 3600000 
    jpa: 
     show_sql: true 

hibernate: 
    dialect: org.hibernate.dialect.MySQLDialect 
    show_sql: false 
    format_sql: true 
    connection: 
     provider_class: com.zaxxer.hikari.hibernate.HikariConnectionProvider 
     release_mode: after_transaction 
... 

ApplicationConfiguration.java

@Configuration 
@PropertySource("classpath:application.yml") 
@EnableTransactionManagement 
@EnableSwagger2 
@EntityScan("com...dal.data") 
public class ApplicationConfiguration extends WebMvcConfigurerAdapter { 

    @Configuration 
    @ConfigurationProperties(prefix="spring.datasource") 
    public class JpaConfig extends HikariConfig {} 

    @Autowired 
    private JpaConfig jpaConfig; 

    @Bean(destroyMethod = "close") 
    public DataSource dataSource() { 
     return new HikariDataSource(jpaConfig); 
    } 

    @Bean 
    public SessionFactory sessionFactory() { 
     LocalSessionFactoryBuilder factoryBuilder = new LocalSessionFactoryBuilder(dataSource()); 
     factoryBuilder.addAnnotatedClasses(
       com...dal.data.MyEntity.class, ... 
     ); 
     return factoryBuilder.buildSessionFactory(); 
    } 

TestDaoImpl.java

@Repository 
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class TestDaoImpl implements TestDao { 

    private static final Logger logger = LoggerFactory.getLogger(TestDaoImpl.class); 

    @PersistenceContext 
    private EntityManager em; 

    @SuppressWarnings("unchecked") 
    @Override 
    public List<MyEntity> getEntities() { 
     return em.unwrap(Session.class) 
       .createCriteria(MyEntity.class, "myEntity") 
       .list(); 
    } 

    @Override 
    @Transactional 
    public void saveTest(MyEntity test) throws OperationException { 
     try { 
      em.persist(test); 
     } catch (Exception e) { 
      logger.error("ERROR saving test", e); 
      throw new OperationException("PS-SERVER"); 
     } 
    } 

回答

0

此代碼運行良好。

的問題是與另一@Repository類的項目在做

@Inject 
private SessionFactory sessionFactory; 

這是吃了連接,即使代碼不會在測試服務調用。我仍然不確定這是如何工作的,但是一旦我用

@PersistenceContext 
private EntityManager em; 

代替它的工作。

+0

我正在使用以下技術來獲取HibernateSession:http://stackoverflow.com/a/33881946/272180並且因爲有一段時間我在應用程序中也遇到了泄漏。它以前工作得很好。奇怪。 – yglodt