使用Hibernate + MYSQL数据库问题(Hibernate连接数据库超时设置autoReconnect=true)

使用Hibernate + MYSQL数据库开发,链接超时问题:

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

 

查了一下,原来是mysql超时设置的问题
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat.

 

 

解决办法:

 

 

    一种. 如果不用hibernate的话, 则在 connection url中加参数: autoReconnect=true

jdbc.url=jdbc:mysql://ipaddress:3306/database?autoReconnect=true&autoReconnectForPools=true

 


    二种。用hibernate的话, 加如下属性:
        <property name="connection.autoReconnect">true</property>
        <property name="connection.autoReconnectForPools">true</property>
        <property name="connection.is-connection-validation-required">true</property>

 


    三。要是还用c3p0连接池:
        <property name="hibernate.c3p0.acquire_increment">1</property>
        <property name="hibernate.c3p0.idle_test_period">0</property>
        <property name="hibernate.c3p0.timeout">0</property>
        <property name="hibernate.c3p0.validate">true</property>

 

 

 四。最不好的解决方案

 

使用Connector/J连接MySQL数据库,程序运行较长时间后就会报以下错误:

Communications link failure,The last packet successfully received from the server was *** millisecond ago.The last packet successfully sent to the server was ***  millisecond ago。

其中错误还会提示你修改wait_timeout或是使用Connector/J的autoReconnect属性避免该错误。

后来查了一些资料,才发现遇到这个问题的人还真不少,大部分都是使用连接池方式时才会出现这个问题,短连接应该很难出现这个问题。这个问题的原因:

MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

1.按照错误的提示,可以在JDBC URL中使用autoReconnect属性,实际测试时使用了autoReconnect=true&failOverReadOnly=false,不过并未起作用,使用的是5.1版本,可能真像网上所说的只对4之前的版本有效。

2.没办法,只能修改MySQL的参数了,wait_timeout最大为31536000即1年,在my.cnf中加入:

[mysqld]

wait_timeout=31536000

interactive_timeout=31536000

重启生效,需要同时修改这两个参数。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
具体实现过程如下: 1. 确定数据传输方式 这里我们使用 WebSocket 进行实时数据传输。使用 WebSocket 的好处是可以保持长连接,实现实时数据传输。 2. Winform 中连接 MySQL 数据库 在 Winform 中连接 MySQL 数据库使用 MySQL.Data 库进行连接。添加代码如下: ``` using MySql.Data.MySqlClient; public void Connect() { string connStr = "server=localhost;user=root;database=test;port=3306;password=123456;"; MySqlConnection conn = new MySqlConnection(connStr); try { conn.Open(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } conn.Close(); } ``` 其中,`server`、`user`、`database`、`port`、`password` 是连接 MySQL 数据库的参数。 3. Web 客服系统中连接 MySQL 数据库 在 Web 客服系统中连接 MySQL 数据库使用 JDBC 进行连接。添加代码如下: ``` String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&autoReconnect=true&failOverReadOnly=false&maxReconnects=10"; String user = "root"; String password = "123456"; Connection conn = DriverManager.getConnection(url, user, password); ``` 其中,`url`、`user`、`password` 是连接 MySQL 数据库的参数。 4. Winform 窗体中添加 WebBrowser 控件 在 Winform 窗体中添加 WebBrowser 控件,用于显示 Web 客服系统页面。添加代码如下: ``` private void Form1_Load(object sender, EventArgs e) { webBrowser1.Navigate("http://localhost:8080/chat"); } ``` 其中,`http://localhost:8080/chat` 是 Web 客服系统的地址。 5. Web 客服系统中添加接口 在 Web 客服系统中添加接口,用于接收 Winform 窗体传来的数据,并进行数据处理和存储。添加代码如下: ``` // 接收消息 @OnWebSocketMessage public void onMessage(Session session, String message) { // 处理消息 // 存储消息 Connection conn = DriverManager.getConnection(url, user, password); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO message(content) VALUES(?)"); pstmt.setString(1, message); pstmt.executeUpdate(); pstmt.close(); conn.close(); } ``` 6. Winform 窗体中使用 WebSocket 连接 Web 客服系统 在 Winform 窗体中使用 C# 的 WebSocket 连接 Web 客服系统,实现数据传输。添加代码如下: ``` var uri = new Uri("ws://localhost:8080/chat"); var ws = new WebSocket(uri); ws.OnMessage += (sender, e) => { // 处理消息 }; ws.Connect(); ``` 7. 数据的实时同步 使用 WebSocket 进行实时数据传输,使得 Winform 窗体和 Web 客服系统之间的数据保持同步。添加代码如下: ``` // Winform 窗体发送消息 ws.Send("hello"); // Web 客服系统发送消息 session.getRemote().sendString("world"); ``` 8. 数据的安全性 在数据传输过程中进行加密和身份验证,以确保数据的安全性。这里我们可以使用 SSL/TLS 进行数据加密,使用 JWT 进行身份验证。 具体实现代码如下: ``` // Winform 窗体连接 Web 客服系统 var uri = new Uri("wss://localhost:8080/chat"); var ws = new WebSocket(uri); ws.OnMessage += (sender, e) => { // 处理消息 }; ws.SslConfiguration.EnabledSslProtocols = SslProtocols.Tls12; ws.SetCredentials("username", "password", false); ws.SetCookie(new WebSocketSharp.Net.Cookie("jwt", "token")); ws.Connect(); ``` 以上就是 Winform+MySQL数据库 和 Web 客服系统对接并实现数据实时同步和传输的具体实现过程和代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值