我正在使用Amazon EC2來運行我的Java Wicket應用程序,並且我已經爲應用程序運行了Amazon MySQL實例。所有的工作正常,但8小時後,我的數據庫連接丟失。我試圖配置c3p0設置,以免發生這種情況。我也嘗試更新MySQL設置,但沒有幫助。MySQL連接和休眠c3p0設置,8小時後超時?
這裏是我的persitence.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="xyz">
<persistence-unit name="mysqldb-ds" transaction-type="RESOURCE_LOCAL">
<description>Persistence Unit</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="XXXXX"/>
<property name="hibernate.connection.password" value="YYYYY"/>
<property name="hibernate.connection.url" value="jdbc:mysql://xxxx.yyyyy.us-east-1.rds.amazonaws.com:3306/paaluttaja"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="connection.pool_size" value="1" />
<property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="300" />
<property name="hibernate.c3p0.max_statements" value="50" />
<property name="hibernate.c3p0.idle_test_period" value="3000" />
</properties>
</persistence-unit>
</persistence>
我提出疑問是這樣的:
@Service
public class LoginServiceImpl implements LoginService{
@Override
public Customer login(String username, String password) throws LSGeneralException {
EntityManager em = EntityManagerUtil.getEm();
TypedQuery<Customer> query = em.createQuery("SELECT c FROM Customer c "
+ "WHERE c.username = '" + username + "' "
+ "AND c.password = '" + password + "'", Customer.class);
Customer customer = null;
try {
customer = (Customer) query.getSingleResult();
}catch(Exception e){
if(e instanceof NoResultException){
throw new LSGeneralException("login.failed.wrong.credentials", e);
}else{
throw new LSGeneralException("failure", e);
}
}
customer.setLogged(true);
return customer;
}
}
所有幫助將不勝感激。
我對配置做了一些更改,現在我們等待並希望最好。監視顯示,hibernate.c3p0.min_size = 5已生效,僅在1之前。監控圖像:http://oi49.tinypic.com/35hipee.jpg – TuomasSaranen 2013-02-22 08:21:04
嗨史蒂夫,你可以看看我最近的問題,它有點相關:http://stackoverflow.com/questions/19664878/configure-c3p0關閉休眠會話時,連接是關閉?謝謝 – amphibient 2013-10-29 20:43:46