提交 e9e0d9bc authored 作者: Sergi Vladykin's avatar Sergi Vladykin

Memory leak fixed in PgServerThread (Prepared and Portal object were never deleted from connection)

上级 086f526f
...@@ -229,11 +229,10 @@ public class PgServerThread implements Runnable { ...@@ -229,11 +229,10 @@ public class PgServerThread implements Runnable {
String prepName = readString(); String prepName = readString();
Prepared prep = prepared.get(prepName); Prepared prep = prepared.get(prepName);
if (prep == null) { if (prep == null) {
sendErrorResponse("Portal not found"); sendErrorResponse("Prepared not found");
break; break;
} }
portal.sql = prep.sql; portal.prep = prep;
portal.prep = prep.prep;
portals.put(portal.name, portal); portals.put(portal.name, portal);
int formatCodeCount = readShort(); int formatCodeCount = readShort();
int[] formatCodes = new int[formatCodeCount]; int[] formatCodes = new int[formatCodeCount];
...@@ -246,7 +245,7 @@ public class PgServerThread implements Runnable { ...@@ -246,7 +245,7 @@ public class PgServerThread implements Runnable {
byte[] d2 = Utils.newBytes(paramLen); byte[] d2 = Utils.newBytes(paramLen);
readFully(d2); readFully(d2);
try { try {
setParameter(portal.prep, i, d2, formatCodes); setParameter(prep.prep, i, d2, formatCodes);
} catch (Exception e) { } catch (Exception e) {
sendErrorResponse(e); sendErrorResponse(e);
} }
...@@ -259,6 +258,25 @@ public class PgServerThread implements Runnable { ...@@ -259,6 +258,25 @@ public class PgServerThread implements Runnable {
sendBindComplete(); sendBindComplete();
break; break;
} }
case 'C': {
char type = (char) readByte();
String name = readString();
server.trace("Close");
if (type == 'S') {
Prepared p = prepared.remove(name);
if (p != null) {
JdbcUtils.closeSilently(p.prep);
}
} else if (type == 'P') {
portals.remove(name);
} else {
server.trace("expected S or P, got " + type);
sendErrorResponse("expected S or P");
break;
}
sendCloseComplete();
break;
}
case 'D': { case 'D': {
char type = (char) readByte(); char type = (char) readByte();
String name = readString(); String name = readString();
...@@ -275,7 +293,7 @@ public class PgServerThread implements Runnable { ...@@ -275,7 +293,7 @@ public class PgServerThread implements Runnable {
if (p == null) { if (p == null) {
sendErrorResponse("Portal not found: " + name); sendErrorResponse("Portal not found: " + name);
} else { } else {
PreparedStatement prep = p.prep; PreparedStatement prep = p.prep.prep;
try { try {
ResultSetMetaData meta = prep.getMetaData(); ResultSetMetaData meta = prep.getMetaData();
sendRowDescription(meta); sendRowDescription(meta);
...@@ -298,8 +316,9 @@ public class PgServerThread implements Runnable { ...@@ -298,8 +316,9 @@ public class PgServerThread implements Runnable {
break; break;
} }
int maxRows = readShort(); int maxRows = readShort();
PreparedStatement prep = p.prep; Prepared prepared = p.prep;
server.trace(p.sql); PreparedStatement prep = prepared.prep;
server.trace(prepared.sql);
try { try {
prep.setMaxRows(maxRows); prep.setMaxRows(maxRows);
boolean result = prep.execute(); boolean result = prep.execute();
...@@ -311,12 +330,12 @@ public class PgServerThread implements Runnable { ...@@ -311,12 +330,12 @@ public class PgServerThread implements Runnable {
while (rs.next()) { while (rs.next()) {
sendDataRow(rs); sendDataRow(rs);
} }
sendCommandComplete(p.sql, 0); sendCommandComplete(prepared.sql, 0);
} catch (Exception e) { } catch (Exception e) {
sendErrorResponse(e); sendErrorResponse(e);
} }
} else { } else {
sendCommandComplete(p.sql, prep.getUpdateCount()); sendCommandComplete(prepared.sql, prep.getUpdateCount());
} }
} catch (Exception e) { } catch (Exception e) {
sendErrorResponse(e); sendErrorResponse(e);
...@@ -586,6 +605,11 @@ public class PgServerThread implements Runnable { ...@@ -586,6 +605,11 @@ public class PgServerThread implements Runnable {
sendMessage(); sendMessage();
} }
private void sendCloseComplete() throws IOException {
startMessage('3');
sendMessage();
}
private void initDb() throws SQLException { private void initDb() throws SQLException {
Statement stat = null; Statement stat = null;
ResultSet rs = null; ResultSet rs = null;
...@@ -807,20 +831,15 @@ public class PgServerThread implements Runnable { ...@@ -807,20 +831,15 @@ public class PgServerThread implements Runnable {
*/ */
String name; String name;
/**
* The SQL statement.
*/
String sql;
/** /**
* The format used in the result set columns (if set). * The format used in the result set columns (if set).
*/ */
int[] resultColumnFormat; int[] resultColumnFormat;
/** /**
* The prepared statement. * The prepared object.
*/ */
PreparedStatement prep; Prepared prep;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论