问题
I've started a project using Hibernate 5 and a MySQL database. Prior to this project, I've been using Hibernate 4, PostgreSQL and C3P0 connection pool.
Now, I want to use HikariCP as it seems really promising, with Hibernate 5.
Here it's my IVY configuration part for hibernate:
<?xml version="1.0"?>
<!DOCTYPE ivy-module [
<!ENTITY vaadin.version "7.5.5">
<!ENTITY highcharts.version "1.1">
<!ENTITY vaadin.charts.version "3.0.0-alpha5">
<!ENTITY hibernate.version "5.0.1.Final">
<!ENTITY mysql.version "5.1.36">
<!ENTITY log4j.version "2.3">
<!ENTITY apache.poi.version "3.12">
<!ENTITY apache.commons.io.version "2.4">
]>
...
<!-- Hibernate -->
<dependency org="org.hibernate" name="hibernate-core" rev="&hibernate.version;" /><!-- core -->
<dependency org="org.hibernate" name="hibernate-java8" rev="&hibernate.version;" /><!-- java 8 support -->
<!-- Connection Pool: -->
<dependency org="org.hibernate" name="hibernate-hikaricp" rev="&hibernate.version;" /><!-- hikariCP -->
My DAO seems like this:
public class PtDao {
private Logger logger = LoggerFactory.getLogger(PtDao.class);
private static PtDao instance;
private Configuration cfg;
private static ServiceRegistry serviceRegistry;
private SessionFactory factory;
private Transaction tx;
private static Session session;
private PtDao(){
prepararConexionConHikariCP();
}
private void prepararConexionConHikariCP(){
try{
cfg = new Configuration();
cfg.setProperty("hibernate.hbm2ddl.auto", "update");
cfg.setProperty("hibernate.transaction.auto_close_session", "true");
cfg.setProperty("hibernate.connection.autocommit", "false");
cfg.setProperty("hibernate.connection.provider_class", "com.zaxxer.hikari.hibernate.HikariConnectionProvider");
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
cfg.setProperty("hibernate.hikari.dataSourceClassName", "com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
// MySQL Servidor Linux
cfg.setProperty("hibernate.hikari.dataSource.url", "jdbc:mysql://192.168.1.101:3306/averiassae");
cfg.setProperty("hibernate.hikari.dataSource.user", "root");
cfg.setProperty("hibernate.hikari.dataSource.password", "XXXXX");
cfg.setProperty("hibernate.hikari.dataSource.cachePrepStmts", "true");
cfg.setProperty("hibernate.hikari.dataSource.prepStmtCacheSize", "250");
cfg.setProperty("hibernate.hikari.dataSource.prepStmtCacheSqlLimit", "2048");
//cfg.setProperty("hibernate.hikari.idleTimeout", "300000");
//cfg.setProperty("hibernate.hikari.minimumIdle", "10");
//cfg.setProperty("hibernate.hikari.maximumPoolSize", "20");
cfg.setProperty("hibernate.show_sql", "true");
cfg.setProperty("hibernate.format_sql", "true");
cfg.setProperty("hibernate.generate_statistics", "true");
anyadirPersistencia();
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
serviceRegistryBuilder.applySettings(cfg.getProperties());
serviceRegistry = serviceRegistryBuilder.build();
factory = cfg.buildSessionFactory(serviceRegistry);
this.openSession();
} catch (HibernateException e) {
logger.error("Error HibernateException (" + e.getClass() + ") en PtDao(): " + e.getMessage() + ". Causa: " + e.getCause());
e.printStackTrace();
}
}
private void anyadirPersistencia() {
cfg.addAnnotatedClass(Operador.class);
cfg.addAnnotatedClass(Averia.class);
}
...
It seems that Hikari is well configured, but once and again new connections appear on MySQL Workbench's Administration-Client connections window, even if the application is stopped...
My questions are:
1) Is Hibernate and Hikari well configured?
2) Any clue to manage those connections?
Thanks for your help
来源:https://stackoverflow.com/questions/32821574/hibernate-5-hikaricp-mysql