Ich bekomme unter Fehler nach 5-6 Anfragen.Frühling Hibernate Connection Leak
Verwenden Sie unten Code funktioniert perfekt, außer es erschöpft den Verbindungspool nach wenigen Anfragen.
Ich bin neu zu Spring-Framework und machte all diese mit Online-Stichproben. Ich habe ein paar Varianten ausprobiert und alles gescheitert. Jede Hilfe wäre willkommen. Vielen Dank.
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");
}
}
Ich verwende die folgende Technik, um die HibernateSession zu bekommen: http://StackOverflow.com/a/33881946/272180 und seit einer Weile bekomme ich auch Lecks in einer Anwendung. Früher hat es gut funktioniert. Seltsam. – yglodt