我发现我的数据库是我的应用程序中的瓶颈,作为这一部分,准备好的语句似乎没有被重用。
例如这里我使用的方法

public static CoverImage findCoverImageBySource(Session session, String src)
{
    try
    {
        Query q = session.createQuery("from CoverImage t1 where t1.source=:source");
        q.setParameter("source", src, StandardBasicTypes.STRING);
        CoverImage result = (CoverImage)q.setMaxResults(1).uniqueResult();
        return result;
    }
    catch (Exception ex)
    {
        MainWindow.logger.log(Level.SEVERE, ex.getMessage(), ex);
    }
    return null;
}

但用你的工具档案显示
com.mchange.v2.c3po.impl.newProxyPreparedStatemTn.executeQuery()计数511
com.mchnage.v2.c3po.impl.newProxyConnection.prepareStatement()计数511
我假设prepareStatement()调用的计数应该更低,因为看起来我们每次都创建一个新的prepareStatement,而不是重用。
https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html
database - 我的H2/C3PO/Hibernate设置似乎不保留准备好的语句?-LMLPHP
我正在使用c3po连接池,这会使事情复杂一些,但据我所知,我已经正确配置了它
公共静态配置getinitializedconfiguration()
{
/参见https://www.mchange.com/projects/c3p0/#hibernate-specific
配置配置=新配置();
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","1");
//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.max_statements","5000");
config.setProperty("hibernate.c3p0.timeout","2000");
config.setProperty("hibernate.c3p0.maxStatementsPerConnection","50");
config.setProperty("hibernate.c3p0.idle_test_period","3000");
config.setProperty("hibernate.c3p0.acquireRetryAttempts","10");
//Cancel any connection that is more than 30 minutes old.
//config.setProperty("hibernate.c3p0.unreturnedConnectionTimeout","3000");
//config.setProperty("hibernate.show_sql","true");
//config.setProperty("org.hibernate.envers.audit_strategy", "org.hibernate.envers.strategy.ValidityAuditStrategy");
//config.setProperty("hibernate.format_sql","true");

config.setProperty("hibernate.generate_statistics","true");
//config.setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
//config.setProperty("hibernate.cache.use_second_level_cache", "true");
//config.setProperty("hibernate.cache.use_query_cache", "true");
addEntitiesToConfig(config);
return config;

}
使用h2 1.3.172、hibernate 4.3.11和该hibernate版本对应的c3po
我们有可重复的测试用例
冬眠状态
hibernatestatics.getQueryExecutionCount()28
hibernatestatics.getEntityInsertCount()119
hibernatestatics.getEntityUpdateCount()39
hibernateStatistics.getPrepareStatementCount()189
探查器,方法计数
googostatementcache.aquirestatement()35
googlestatementcache.checkinstatement()189
googostaementcache.checkOutstatement()189
newProxyPreparedStatement.init()189
我不知道我应该把什么当作是准备好的陈述而不是重用现有的准备陈述?
我还尝试通过添加一个c3p0记录器来启用c3p0日志记录,并使其在我的logproperties中使用相同的日志文件,但没有效果。
            String logFileName = Platform.getPlatformLogFolderInLogfileFormat() + "songkong_debug%u-%g.log";
            FileHandler fe = new FileHandler(logFileName, LOG_SIZE_IN_BYTES, 10, true);
            fe.setEncoding(StandardCharsets.UTF_8.name());
            fe.setFormatter(new com.jthink.songkong.logging.LogFormatter());
            fe.setLevel(Level.FINEST);

            MainWindow.logger.addHandler(fe);

            Logger c3p0Logger = Logger.getLogger("com.mchange.v2.c3p0");
            c3p0Logger.setLevel(Level.FINEST);
            c3p0Logger.addHandler(fe);

最佳答案

现在我终于可以使用基于c3p0的日志了,我可以确认@stevewaldman的建议是正确的。
如果启用

public static  Logger  c3p0ConnectionLogger = Logger.getLogger("com.mchange.v2.c3p0.stmt");
c3p0ConnectionLogger.setLevel(Level.FINEST);
c3p0ConnectionLogger.setUseParentHandlers(false);

然后得到表单的日志输出
24/08/2019 10.20.12:BST:FINEST: com.mchange.v2.c3p0.stmt.DoubleMaxStatementCache ----> CACHE HIT
24/08/2019 10.20.12:BST:FINEST: checkoutStatement: com.mchange.v2.c3p0.stmt.DoubleMaxStatementCache stats -- total size: 347; checked out: 1; num connections: 13; num keys: 347
24/08/2019 10.20.12:BST:FINEST: checkinStatement(): com.mchange.v2.c3p0.stmt.DoubleMaxStatementCache stats -- total size: 347; checked out: 0; num connections: 13; num keys: 347

当你的缓存被命中时,你要说清楚。当没有缓存命中时,不要得到第一行,而是得到另外两行。
这是使用c3p0 9.2.1

07-28 08:20