首页 新闻 搜索 专区 学院

写了一个循环读取客户端信息并上传到数据库的程序,但是运行一段时间后总是报错

0
悬赏园豆:60 [待解决问题]


com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:1853)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:1558)
at ServerThread.run(ServerThread.java:136)

 

 

com.microsoft.sqlserver.jdbc.SQLServerException: 该连接已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:1853)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createStatement(SQLServerConnection.java:1558)
at ServerThread.run(ServerThread.java:136)

问题补充:

下面是我的代码,运行一段时间总是报这个错误

分割线割线割线割线割线割线割线割线割线割线割线割线割线割线割线割线割线割线割线

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;

public class ServerThread extends Thread {
Socket socket = null;
private final static String URL = "jdbc:sqlserver://XXXXXXXXX;DatabaseName=XXX_DB";
private static final String USER = "XXXX";//sa
private static final String PASSWORD = "XXXXXXXX";//123,qwe
InetAddress inetAddress=null;//接收客户端的连接
// ServerThread d = new ServerThread(socket, inetAddress);
private static Connection conn=null;

//静态代码块(将加载驱动、连接数据库放入静态块中)
static{
try {

//1.加载驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.获得数据库的连接
conn=(Connection)DriverManager.getConnection(URL,USER,PASSWORD);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}


//对外提供一个方法来获取数据库连接
public static Connection getConnection(){
return conn;
}

//插入
public void insert(Statement stmt,String sql) throws SQLException {
//成功-返回false
// boolean execute = stmt.execute("insert into 李运辰.选课 values(1,2,100)");

boolean execute = stmt.execute(sql);
System.out.println(execute);
}
/////////////////////////////////////////////
public ServerThread(Socket socket,InetAddress inetAddress) {
this.socket = socket;
this.inetAddress=inetAddress;
}

@Override
public void run() {
InputStream inputStream = null;//字节输入流
InputStreamReader inputStreamReader = null;//将一个字节流中的字节解码成字符
BufferedReader bufferedReader = null;//为输入流添加缓冲
OutputStream outputStream = null;//字节输出流
OutputStreamWriter writer = null;//将写入的字符编码成字节后写入一个字节流
DataInputStream in = null;
try {
inputStream = socket.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String info = null;//临时

//循环读取客户端信息
while ((info = bufferedReader.readLine()) != null) {
//获取客户端的ip地址及发送数据
System.out.println("服务器端接收:"+"{'from_client':'"+socket.getInetAddress().getHostAddress()+"','data':'"+info+"'}");


in = new DataInputStream(socket.getInputStream());
byte[] recvBytes = new byte[2048];
String TagID;
String Status;
String temp;
String rep_TagID = null;
// GAO217001Sample d = new GAO217001Sample();
// Statement stmt = conn.createStatement();
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
do
{

in.read(recvBytes);

//Judge data head
if ((recvBytes[0]==66)
&&(recvBytes[1]==73)
&&(recvBytes[2]==83)
&&(recvBytes[3]==65)
&&(recvBytes[4]==95)
&&(recvBytes[5]==82)
&&(recvBytes[6]==70)
&&(recvBytes[7]==73)
&&(recvBytes[8]==68)
&&(recvBytes[9]==0))
{
//Judge data package type
if ((recvBytes[14]==1) || (recvBytes[14]==21) && (recvBytes[15]==0))
{
//starting from byte[48], every 13 byte is a reading record
for (int i=0;i<(recvBytes[13]*256+recvBytes[12]-48)/13;i++)
{
TagID = "";
Status = "";

//Find Tag ID;
for (int j=0;j<8;j++)
{
temp = Integer.toHexString(recvBytes[i*13+j+48]);
if (temp.length() == 1)
temp = "0"+temp;
TagID = TagID + temp;
}

//Find Status
Status = Integer.toHexString(recvBytes[i*13+8+48]);
if(TagID.contains("fff")) {
//System.out.println("含ff的TagID:"+TagID);
// System.out.println("去掉ff的TagID:"+TagID.replace("ffffff", ""));
rep_TagID = TagID.replace("ffffff", "");
}
else {
System.out.println("不含ff的TagID:"+TagID);
rep_TagID=TagID;
}

System.out.println("Status:"+Status+"\r\n");
ServerThread d = new ServerThread(socket, inetAddress);
Statement stmt = conn.createStatement();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String ss = "insert into R_data(tagid,state,Riftime,ReaderIP) values('"+rep_TagID+"','"+Status+"',getdate(),'"+socket.getInetAddress().getHostName()+"')";
System.out.println(df.format(System.currentTimeMillis())+": "+ss);
d.insert(stmt, "insert into R_data(RCode,tagid,state,Riftime,ReaderIP) values('001002','"+rep_TagID+"','"+Status+"',getdate(),'"+socket.getInetAddress().getHostName()+"')");
stmt.close();
}
}
}
}
while(true);



}

socket.shutdownInput();//关闭输入流

//响应客户端请求
// outputStream = socket.getOutputStream();
// writer = new OutputStreamWriter(outputStream, "UTF-8");
// writer.write("{'to_client':'"+inetAddress.getHostAddress()+"','data':'我是服务器数据'}");
// writer.flush();//清空缓冲区数据
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//关闭资源
try {
if (writer != null) {
writer.close();
}
if (outputStream != null) {
outputStream.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (inputStream != null) {
inputStream.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

哦~哦的主页 哦~哦 | 初学一级 | 园豆:102
提问于:2020-10-20 08:55
< >
分享
所有回答(3)
0

session有及时关闭么?应该不能保持长时间连接。或者百度一下,如何保持长时间连接

明日小路 | 园豆:527 (小虾三级) | 2020-10-20 08:57

我这个不是session,百度说应该把Connection conn设置为一个局部变量,我设置了一下还是不行

支持(0) 反对(0) 哦~哦 | 园豆:102 (初学一级) | 2020-10-20 17:21

我说错了,应该是 Connection 是否关闭? 你是用传统JDBC写的吗? 试试每次写完,就把 conn 关闭。

支持(0) 反对(0) 明日小路 | 园豆:527 (小虾三级) | 2020-10-20 17:23

....你是初学者吗?其实其它网友也提到了:关闭链接。你的链接在静态代码块里面实例化了;你现在要把它改为:每执行一段数据库操作之前,就开启链接,执行完毕之后,关闭链接。你先试试

支持(0) 反对(0) 明日小路 | 园豆:527 (小虾三级) | 2020-10-23 14:15

@南小鸟: 好的

支持(0) 反对(0) 哦~哦 | 园豆:102 (初学一级) | 2020-10-23 16:49
0

并发的情况下,读和插入不能一起哈,你要调整你 mysql的事务级别,或者多弄几个连接池

小小咸鱼YwY | 园豆:3191 (老鸟四级) | 2020-10-20 09:17
0

连接关闭了。可以在调用的地方检查一下连接是否关闭,关闭了再重新连接一下

通信的搞程序 | 园豆:1742 (小虾三级) | 2020-10-20 09:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册