在长时间空闲后,我遇到了与 Mysql DB 连接丢失的问题,即我收到不同类型的错误,包括“允许的最大数据包大小”错误、“最后一个数据包在 8499970 毫秒前收到”错误等。 我将 Hibernate-JPA 与 JPA 版本 2.0 一起使用
目前我的 Wildflystandalone.xml 中的数据源配置是:
<datasources>
<datasource jndi-name="java:/MySQLDS" pool-name="MySQLDS" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/opera</connection-url>
<driver>mysqlDriver</driver>
<security>
<user-name>admin</user-name>
<password>admin</password>
</security>
</datasource>
<drivers>
<driver name="mysqlDriver" module="com.mysql">
<xa-datasource-class>com.mysql.jdbc.Driver</xa-datasource-class>
</driver>
</drivers>
</datasources>
persistence.xml 文件如下所示:
<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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="metadatatool-persistence">
<jta-data-source>java:/MySQLDS</jta-data-source>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<!-- Hibernate properties -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5" />
<property name="hibernate.c3p0.max_size" value="20" />
<property name="hibernate.c3p0.timeout" value="600" />
<property name="hibernate.c3p0.max_statements" value="2000" />
<property name="hibernate.c3p0.idle_test_period" value="30" />
<property name="hibernat.c3p0.automaticTestTable" value="conTestTable" />
</properties>
</persistence-unit>
</persistence>
我已将 c3p0-4.1.1-final Jar 包含在我的项目中,但它看起来不起作用,在空闲时间后我仍然没有收到连接错误,并且我在任何地方都找不到正确的逐步解决方案
编辑
我在 persistence.xml 中定义了另一个属性,我不知道它是否有效
<property name="hibernate.connection.url"
value="jdbc:mysql://192.168.1.1:3306/opera?zeroDateTimeBehavior=convertToNull&autoReconnect=true;reconnectAtTxEnd=true;autoReconnectForPools=true" />
<property name="hibernate.connection.username" value="root" />
编辑更新 我使用了上面定义的属性,现在没有收到连接错误,但是我确实在下面的日志文件中发现了错误,但部署工作正常
error is: java.lang.IllegalStateException: UT000010: Session not found BDnAhOKBakcD8h9izG1Dembs0DFHWwWgYFVYPEKm
此外,我在此属性中使用的连接字符串是正确的方法吗?我会面临哪些副作用?
最佳答案
在任何完整的 Java EE 实现中,例如 WildFly/JBoss EAP、WebSphere、GlassFish 等,都不需要配置自己的连接池实现(例如 c3po)。
所有这些服务器都为您提供连接池实现和管理工具来配置它们。
在 WildFly 中,您可以使用管理控制台中的数据源配置或使用 CLI 进行设置。请参阅WildFly 10 DataSource configuration .
您应该能够将 persistence.xml 减少为:
<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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="metadatatool-persistence">
<jta-data-source>java:/MySQLDS</jta-data-source>
<properties>
<!-- Hibernate properties -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
</properties>
</persistence-unit>
</persistence>
为了方便起见,我将 SQL 生成属性留在那里,尽管在始终 false
时它们并不是必需的。
此后,您需要确保您的数据源配置正确。
<datasource jta="true"
jndi-name="java:/MySQLDS"
pool-name="MySQLDS" enabled="true" use-ccm="true">
<connection-url>jdbc:mysql://localhost:3306/opera</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver> mysqlDriver </driver>
<security>
<user-name>admin</user-name>
<password>admin</password>
</security>
<validation>
<valid-connection-checker
class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<background-validation>true</background-validation>
<exception-sorter
class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
请注意您缺少的连接验证配置。这是通过管理控制台设置数据源时免费添加的。
您永远不应该手动修改standalone.xml 文件。
关于java - 如何在wildfly 10standalone.xml中定义hibernate.c3p0连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40887995/