简体   繁体   English

MySQL连接和hibernate c3p0设置,8小时后超时?

[英]MySQL connection and hibernate c3p0 settings, timeout after 8 hours?

I am using Amazon EC2 to run my Java Wicket app and I have Amazon MySQL instance running for the application. 我正在使用Amazon EC2来运行我的Java Wicket应用程序,并且我已经为该应用程序运行了Amazon MySQL实例。 All works fine, but after 8 hours my database connection is lost. 一切正常,但8小时后我的数据库连接丢失了。 I have tried to configure c3p0 settings so that this would not happen. 我试图配置c3p0设置,这样就不会发生这种情况。 I have also tried to update MySQL settings, but no help. 我也尝试更新MySQL设置,但没有帮助。

Here is my persitence.xml 这是我的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>

I am making queries like this: 我正在进行这样的查询:

@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;

}

}

All help would be appreciated. 所有帮助将不胜感激。

First, you might want to check your logs for c3p0's dump of its config; 首先,您可能需要检查日志中c3p0的配置转储; your 5-minute timeout should prevent the MySQL connections from going stale after 8 hours, but for some reason that seems not to be happening for you. 你的5分钟超时应该可以防止MySQL连接在8小时后失效,但由于某种原因,似乎没有发生在你身上。 You want to see if the c3p0 property 'maxIdleTime' is actually 300 as expected. 你想看看c3p0属性'maxIdleTime'是否实际上是300预期的。 Anyway, you might try adding 无论如何,你可能会尝试添加

hibernate.c3p0.preferredTestQuery=SELECT 1
hibernate.c3p0.testConnectionOnCheckout=true

this is the simplest way to ensure the validity of Connections -- test them (with an efficient query) on every checkout. 这是确保Connections有效性的最简单方法 - 在每次结账时测试它们(使用有效的查询)。 it is also relatively expensive; 它也相对昂贵; if you find that's a problem, you can go to something savvier. 如果你发现这是一个问题,你可以去更精明的东西。 But it'd be good to make sure you can get a reliable set-up, and then optimize from there. 但确保您可以获得可靠的设置,然后从那里进行优化是很好的。 See here . 看到这里

Note that your hibernate.c3p0.idle_test_period=3000 is not useful, if your pool is configured as you think it is. 请注意,如果您的池配置为您认为的那样,则hibernate.c3p0.idle_test_period=3000无用。 Idle connections will be timed out long before the 3000-second test interval has passed. 空闲连接将在3000秒测试间隔过去之前很久就会超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM