【发布时间】:2019-02-09 21:06:42
【问题描述】:
我正在使用 Hibernate 4.3.11.Final 和 H2 1.3.172 数据库,我在一个缓慢的 linux 机器上分析我的应用程序,发现它在特定 SQL INSERT 上花费的时间比其他任何东西都多。似乎准备好的语句没有被缓存,因为准备好的语句的数量似乎与执行的语句数量大致相同。
我的解释是否正确(我正在使用 Yourkit Profiler)
我的 HibernateUtil 类配置如下
public static Configuration getInitializedConfiguration()
{
Configuration config = new Configuration();
config.setProperty(Environment.DRIVER,"org.h2.Driver");
config.setProperty(Environment.URL,"jdbc:h2:"+Db.DBFOLDER+"/"+Db.DBNAME+";FILE_LOCK=SOCKET;MVCC=TRUE;DB_CLOSE_ON_EXIT=FALSE;CACHE_SIZE=50000");
config.setProperty(Environment.DIALECT,"org.hibernate.dialect.H2Dialect");
System.setProperty("h2.bindAddress", InetAddress.getLoopbackAddress().getHostAddress());
config.setProperty("hibernate.connection.username","jaikoz");
config.setProperty("hibernate.connection.password","jaikoz");
config.setProperty("hibernate.c3p0.numHelperThreads","10");
config.setProperty("hibernate.c3p0.min_size","20");
//Consider that if we have lots of busy threads waiting on next stages could we possibly have alot of active
//connections.
config.setProperty("hibernate.c3p0.max_size","200");
config.setProperty("hibernate.c3p0.timeout","300");
config.setProperty("hibernate.c3p0.maxStatementsPerConnection","50");
config.setProperty("hibernate.c3p0.idle_test_period","3000");
config.setProperty("hibernate.c3p0.acquireRetryAttempts","10");
addEntitiesToConfig(config);
return config;
}
我想知道我是否配置错误,特别令人困惑的是 c3po 文档在某些参数的名称方面确实与 Hibernate 配置完全结合。
即它是 max_size 或 max_pool_size
它是一个单用户多线程应用程序,理想情况下,我希望在应用程序期间缓存所有准备好的语句,因为只有大约 50 条不同的语句。
每次我都明白这一点
session = HibernateUtil.beginTransaction();
这将从池中获得一个连接,并且如果该特定连接先前已经准备好现在需要的语句,那么它可以使用该准备好的语句而无需编译新的语句。
如果准备好的语句不存在,则准备好。
如果这个连接已经有 50 个准备好的语句,那么最旧的语句将被丢弃。
这个占用更多时间的特定查询如下使用
public static void saveMatchedToRelease(Session session,Integer reportId, Integer recNo, SongFieldKey songFieldKey, SongChangeType type, String original, String edited)
{
SongChanges sc = new SongChanges();
sc.setReportId(reportId);
sc.setRecNo(recNo);
sc.setField(songFieldKey);
sc.setType(type);
sc.setOriginalValue(original);
sc.setNewValue(edited);
session.save(sc);
}
【问题讨论】:
-
只是检查一下,因为它在屏幕截图中并不明显 - 你有
hibernate-c3p0工件作为依赖项吗?在 Hibernate 的早期版本中,org.hibernate.c3p0.internal.C3P0ConnectionProvider类在主要的hibernate.jar中提供,因此许多 Hibernate/C3P0 指令没有提及额外的依赖关系。 -
@df778899 嗯,我不知道,我认为它会作为 Hibernate 的一部分包含在内,我是否必须明确添加它 - 我使用的是 maven?
-
我希望如此 -
hibernate-c3p0不是hibernate-core的传递依赖。虽然再次查看屏幕截图,但它显示了来自 C3P0 的较低级别的NewProxyConnection类等。我猜的另一种可能性是hibernate-core.jar和较旧的hibernate.jar都存在于依赖关系树的某个位置。如果这是针对较新的 Hibernate 获取过时的C3P0ConnectionProvider,则可能无法通过设置。
标签: java hibernate profiler c3p0