javaweb应用程序的Hibernate

javaweb应用程序的Hibernate,hibernate,jpa,Hibernate,Jpa,我将Hibernate与c3p0一起使用(用于连接池) 我已经使用persistence.xml文件设置了连接详细信息。这是我的persistence.xml文件 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in

我将Hibernate与c3p0一起使用(用于连接池)

我已经使用persistence.xml文件设置了连接详细信息。这是我的persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_2_0.xsd">
<persistence-unit name="LBSV1.0" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>xx.dataobject.CompanyDO</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.connection.url" value="jdbc:mysql://xx.xx.xx.xx:xxxx/lbs?autoReconnect=true"/>
        <property name="hibernate.connection.username" value="username"/>
        <property name="hibernate.connection.password" value="password"/>
        <property name="hibernate.generate_statistics" value="true"/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.c3p0.min_size" value="5"/>
        <property name="hibernate.c3p0.max_size" value="20"/>
        <property name="hibernate.c3p0.timeout" value="1800"/>
        <property name="hibernate.c3p0.max_statements" value="50"/>
        <property name="hibernate.c3p0.idle_test_period" value="300"/>
        <property name="c3p0.preferredTestQuery" value="select * from status"/>
        <property name="c3p0.testConnectionOnCheckout" value="true"/>
        <property name="c3p0.debugUnreturnedConnectionStackTraces" value="true"/>

    </properties>
</persistence-unit>
在URL中添加了建议的“autoReconnect=true”。我还在谷歌上搜索了连接超时问题,并在“persistence.xml”文件中添加了所需的属性值,但错误仍然存在

以下是c3p0的初始化日志条目:

06 Feb 2013 12:00:56 149355789 [http-8080-1] INFO  com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource  - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@9263d39c [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@77f9bf08 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> true, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsx8s1cc19kywfwqwq|1bdcbb2, idleConnectionTestPeriod -> 300, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@7a7a7ef8 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> z8kfsx8s1cc19kywfwqwq|1b8737f, jdbcUrl -> jdbc:mysql://**.**.**.**:****/lbs?autoReconnect=true, properties -> {user=******, password=******, autocommit=true, release_mode=auto} ], preferredTestQuery -> select * from status, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> z8kfsx8s1cc19kywfwqwq|68097d, numHelperThreads -> 3 ]
有人能告诉我吗

  • persistence.xml文件中的设置有什么问题

  • 或者Hibernate是否在连接设置上产生问题


  • 就好像连接池有一个对连接的引用,但是当它实际尝试使用它时-发现连接无效!:-O

    因此,我认为数据库正在超时连接,而连接池对此一无所知

    问题中的一段文字可能表示答案:

    "is longer than the server configured value of 'wait_timeout'"
    
    因此,我认为您需要查看MySql中配置的等待超时设置。请记住,您正在配置连接池大小(例如hibernate.c3p0.max_size=20),但MySql数据库具有等效的设置。所以(好吧,实际上不确定MySql中是否有等效的连接——这里只是原则性地说一下)如果您的MySql数据库被配置为只允许10个连接,那么连接池将永远不会增长到您配置的最大大小

    您可以在您使用的MySql.cnf文件中找到“wait\u timeout”设置

    在Linux下:

    cat /etc/mysql/my.cnf | grep -i wait
    
    默认值为28800(8小时)-因此您需要增加它。让我们想想这个。假设有人工作到下午5点,第二天早上8点就回来工作了,那已经是15个小时了。因此,这可以为您提供一个指南,说明您需要将其设置为什么值

    旁白-是的,它真的很糟糕,有一个错误被抛出到应用程序。。。但是,我认为增加等待超时时间是一个很好的做法。即使没有抛出错误:假设你有一百万人;)早上8点开始工作…实现很可能很难在几分钟内获得100万个新连接(昂贵的操作),因此在涉及多个连接的情况下,不能允许连接过期

    当我遇到同样的问题时,我做了一些进一步的说明(但与您的设置无关,但可能对其他人有用)

    数据库连接由c3p0管理。数据库不得过早超时c3p0管理的连接。
    (a) “检查数据库上的系统配置。MySQL有一个等待超时设置,它将在一段时间内关闭空闲连接。如果空闲测试超时和超时大于MySQL等待超时(默认为28800秒),您未使用的连接将有机会在测试之前由MySQL服务器重置,或由C3p0超时。“
    (b) “确保此处的值100小于mysql的等待超时设置(默认为8小时)
    
    <你的意思是JPA,而不是OpenJPA?我用OpenJPA jar来做JPA。是Apache软件基金会的JPA的实现。我知道OpenJPA是什么,这就是为什么我问了你的问题。在你的坚持中。XML是很明显的,你正在使用Hibernate,而不是OpenJPA。谢谢你的回复。我知道为什么会出错,但我是Purple。因为即使添加了所有必需的设置,它仍然会给我超时错误。我解决了添加
    “debugUnreturnedConnectionStackTraces”
    “hibernate.c3p0.idle\u test\u period”时出现的错误“
    。这两个设置转储了应用程序中未释放连接的堆栈日志。修复后,应用程序现在不会给出超时错误。
    cat /etc/mysql/my.cnf | grep -i wait
    
    Database connections are managed by c3p0. The database must not prematurely timeout connections managed by c3p0.
        (a) "Check the system configuration on your database. MySQL has a wait_timeout setting which will close the connections idle over a period of time. If your idle_test_period and timeout are greater than MySQL wait_timeout (28800 seconds by default), your unused connections will have chance to be reset by MySQL server before tested or timed out by C3p0."
        (b) "Make sure the value here <property name="hibernate.c3p0.idle_test_period">100</property> which is 100 is less than the wait_timeout set for mysql (by default it is 8 hrs