[英]Losing connection to MySQL after a while, and not reconnecting
I'm developing a standalone server which uses JPA+Hibernate to access a MySQL database. 我正在开发一个使用JPA + Hibernate访问MySQL数据库的独立服务器。
When I start the server, everything is working fine. 当我启动服务器时,一切正常。 However, after a while (usually the next morning, if I start it in the afternoon) it will stop working because apparently the connection to MySQL was closed (I see lots of
SocketException
s in the logs). 然而,过了一段时间(通常是第二天早上,如果我在下午开始),它将停止工作,因为显然与MySQL的连接已关闭(我在日志中看到很多
SocketException
)。 This is probably caused by idling, the server is in development and nobody uses it at night. 这可能是由于空闲,服务器正在开发而没有人在晚上使用它。
I thought Hibernate, JDBC or some other layer below my app would manage the connection and reopen it if neccessary, but apparently not. 我认为Hibernate,JDBC或我应用程序下面的其他一些层将管理连接并在必要时重新打开它,但显然不是。 Is there a configuration parameter I missed?
我错过了配置参数吗?
persistence.xml
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“version =”2.0“>
<persistence-unit name="manager">
<class>example.data.entities.User</class>
<class>example.data.entities.Player</class>
<properties>
<property name="hibernate.dialect" value="example.data.HibernateDialect" />
<property name="hibernate.max_fetch_depth" value="3" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
</properties>
</persistence-unit>
EntityManagerFactory
creation EntityManagerFactory
创建
log.info("Connecting to database @ " + dbUrl + " using " + dbUser + "/" + dbPass);
emf = Persistence.createEntityManagerFactory("manager", Maps.create(
"javax.persistence.jdbc.user", dbUser,
"javax.persistence.jdbc.password", dbPass,
"javax.persistence.jdbc.url", dbUrl
));
A query 一个问题
try
{
TypedQuery<User> q = em.createQuery("SELECT u FROM User u WHERE u.email = :mail", User.class);
q.setParameter("mail", email);
try {
u = q.getSingleResult();
log.info("Authenticating: " + u);
} catch (NoResultException e) {
return false;
}
} finally {
em.close();
}
As you suggest, it is because mysql closes idle connections after each wait_timeout
passes; 正如你的建议,这是因为mysql在每次
wait_timeout
通过后关闭空闲连接; you have some options to work-around your problem: 你有一些选择来解决你的问题:
wait_timeout
in mysql large enough for your use case (default is 8 hrs). wait_timeout
足够大的用例(默认为8小时)。 mysql driver supports autoReconnect the jdbc url will look like jdbc:mysql://localhost/bms_company?autoReconnect=true mysql驱动支持autoReconnect jdbc url看起来像jdbc:mysql:// localhost / bms_company?autoReconnect = true
if u suspect mysql is dropping connections Try adding the following entries at the end of MySQL's my.ini file. 如果你怀疑mysql正在丢弃连接尝试在MySQL的my.ini文件末尾添加以下条目。 This file is located in the MySQL installation's root folder.
该文件位于MySQL安装的根文件夹中。
#The number of seconds the mysqld server is waiting for a connect packet before responding with 'Bad handshake' connect_timeout=0 #在响应“Bad handshake”connect_timeout = 0之前,mysqld服务器等待连接数据包的秒数
#The number of seconds the server waits for activity on an interactive connection before closing it. #服务器在关闭之前等待交互式连接上的活动的秒数。 interactive_timeout=2000000
interactive_timeout = 2000000
#The number of seconds the server waits for activity on a connection before closing it wait_timeout=2000000 #服务器在关闭连接之前等待连接活动的秒数wait_timeout = 2000000
--Kiran.Kumar --Kiran.Kumar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.