我知道这个问题已被多次询问和回答,但没有解决方案可以解决这个问题。
在生产中我得到了这样的错误“从服务器成功收到的最后一个数据包是109,253毫秒之前。”。所以我选择了c3p0并在persistence.xml中进行了以下配置,并且测试将my.cnf中mysql的属性设置为wait_timeout=60
并在设置下使用。
<properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.show_sql" value="true"></property>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/Test"/>
<property name="hibernate.connection.user" value="****" />
<property name="hibernate.connection.password" value="***" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.hibernate.auto_close_session" value="true" />
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.acquire_increment" value="1"/>
<property name="hibernate.c3p0.idle_test_period" value="10"/>
<property name="hibernate.c3p0.min_size" value ="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="50"/>
<property name="hibernate.c3p0.preferredTestQuery" value="select 1"/>
<property name="hibernate.c3p0.testConnectionOnCheckout" value="true"/>
</properties>
即使在日志中,我也看到了
Nov 20, 2014 1:06:35 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
Nov 20, 2014 1:06:35 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@1e8ef9ca [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@1f5f812 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsx951kn0e3017dmiyd|24ba9ccb, idleConnectionTestPeriod -> 10, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 50, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 75, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@fa4d1db0 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> z8kfsx951kn0e3017dmiyd|29c3481b, jdbcUrl -> jdbc:mysql://localhost/DAL, properties -> {user=******, password=******} ], preferredTestQuery -> select 1, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> z8kfsx951kn0e3017dmiyd|126d8fa6, numHelperThreads -> 3 ]
但仍然遇到同样的问题。我尝试过很多东西,甚至在类路径中创建了hibrenate.properties,因为我读了一些属性,比如需要在属性文件中设置preferredTestQuery
等属性,所以我在类路径中创建了一个具有以下属性的属性文件
hibernate.c3p0.idle_test_period=10
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=75
hibernate.c3p0.max_statements=75
hibernate.c3p0.timeout=50
hibernate.c3p0.preferredTestQuery=select 1
hibernate.c3p0.testConnectionOnCheckout=true
甚至属性都按照我可以看到的日志
进行加载org.hibernate.cfg.Environment - HHH000205: Loaded properties from resource hibernate.properties: {hibernate.c3p0.testConnectionOnCheckout=true, hibernate.c3p0.timeout=50, hibernate.c3p0.min_size=5, hibernate.c3p0.max_size=75, hibernate.bytecode.use_reflection_optimizer=false, hibernate.c3p0.preferredTestQuery=select 1, hibernate.c3p0.max_statements=75, hibernate.c3p0.idle_test_period=10
但如果我在一分钟后尝试,仍会得到相同的错误。我无法理解我所缺少的东西。请告诉我?
答案 0 :(得分:2)
您的应用程序可能会保持连接打开,而不是立即将它们返回池中。
要对此进行测试,请尝试设置unreturnedConnectionTimeout
和debugUnreturnedConnectionStackTraces
。有关详细信息,请参阅here。
然后检查您的日志。如果Connections超时未返回,请查看已打开Connections的代码路径的已记录堆栈跟踪,并修改应用程序代码以及时可靠地关闭()这些Connections,并根据需要从DataSource获取Connections -in-时间。
答案 1 :(得分:0)
我遇到同样的问题,解决方法是为mysql添加function getImage(){
var classElements = document.querySelectorAll("tr.ui-selected td.filename");
var csrf = $('input[name=_token]').val();
var result;
result = classElements[0].innerHTML;
$.ajax({
async: true,
method: 'POST',
dataType: 'json',
url: '../public/selecteduserfiles',
data: {filename: result, "_token": csrf},
complete: function(response) {
console.log(response.responseJSON);
$("img.getImage").attr('src', response.responseJSON);
}
});
});
值validationQuery