【发布时间】:2016-11-30 12:08:56
【问题描述】:
长时间空闲后,我遇到了与 Mysql DB 连接断开的问题,即我收到不同类型的错误,包括“允许的最大数据包大小”错误、“在 8499970 毫秒前收到的最后一个数据包”错误等。 我正在将 Hibernate-JPA 与 JPA 2.0 版一起使用
目前我的 Wildfly Standalone.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
我在这个属性中使用的连接字符串也是正确的方法吗?我会面临哪些副作用?
【问题讨论】:
-
为什么您使用 xa-datasource-class ,因为您不使用 xa-datasource 并且只有一个持久性单元?
-
这是我在他们的一个文档中找到的关于如何在 wildfly 中定义数据源的设置,另一个步骤是将 MySqlConnectorJ 放在“模块”目录下
标签: java mysql hibernate jpa wildfly