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

标签 java mysql hibernate c3p0

我正在使用 Amazon EC2 来运行我的 Java Wicket 应用程序,并且我为该应用程序运行了 Amazon MySQL 实例。一切正常,但 8 小时后我的数据库连接丢失。我试图配置 c3p0 设置,这样就不会发生这种情况。我也尝试过更新 MySQL 设置,但没有帮助。

这是我的 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;

}

}

我们将不胜感激。

最佳答案

首先,您可能想检查日志中是否有 c3p0 的配置转储;您的 5 分钟超时应该可以防止 MySQL 连接在 8 小时后失效,但由于某种原因,这似乎不会发生在您身上。您想要查看 c3p0 属性“maxIdleTime”是否确实如预期的那样为 300。无论如何,您可以尝试添加

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

这是确保连接有效性的最简单方法——在每次结帐时测试它们(使用高效查询)。它也相对昂贵;如果你发现这是一个问题,你可以去做一些更聪明的事情。但最好确保您可以获得可靠的设置,然后从那里进行优化。 See here .

请注意,如果您的池配置如您所想,则您的 hibernate.c3p0.idle_test_period=3000 没有用。空闲连接将在 3000 秒测试间隔过去很久之前超时。

关于java - MySQL连接和hibernate c3p0设置,8小时后超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14978617/

相关文章:

php - 在表中输入高级 SQL 查询 - 不是唯一的表别名 mysql

Java泛型继承

java.lang.ClassCastException : java. lang.String 无法转换为 [Ljava.lang.Object;当尝试通过 Hibernate 将列值获取到列表时

java - ConcurrentLinkedDeque的正确使用

mysql - 在 case 循环 mysql 中创建多个数学语句

java - 由 : org. 引起的 hibernate.MappingException: Could not determine type for

java - JVM 是如何决定 JIT 编译方法的(将方法归类为 "hot")?

mysql - 将数据插入mysql字段为空

Hibernate JoinColumn 默认名称缺失 '_id'

java - 如何使用 Hibernate 和 Spring Data JPA 正确注释两个实体之间的关系?