📄 poolelement.java
字号:
/**
* Copyright (C) 2003 Manfred Andres
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package freecs.auth.sqlConnectionPool;
import freecs.Server;
import java.sql.*;
public class PoolElement {
java.sql.Connection con = null;
int statements, sCnt=0, id;
long validUntil;
boolean isActive = false;
Statement read, write;
PoolElement (Connection con, int statements, long validUntil, int id) throws Exception {
if (con == null) throw new Exception ("no connection supplied");
this.id = id;
this.con = con;
con.setAutoCommit (false);
this.statements = statements;
if (validUntil == 0) this.validUntil = 0;
else this.validUntil = System.currentTimeMillis () + validUntil;
initRead ();
initWrite ();
Server.log ("PoolElement.construct: Created new Connection", Server.MSG_STATE, Server.LVL_MINOR);
}
/**
* mark this PoolElement as active
*/
synchronized void setActive () {
isActive = true;
}
/**
* initialize the write-cursor of this PoolElement
* @throws Exception
*/
private void initWrite () throws Exception {
write = con.createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
// write.setCursorName ("write"+id);
}
/**
* initialize the read-cursor of this PoolElement
* @throws Exception
*/
private void initRead () throws Exception {
read = con.createStatement (ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
read.setMaxRows(2);
}
/**
* get the Result for the given parameters
* @param t the table name
* @param data the data-columns to retrieve
* @param cond the condition to match
* @return the resultset containing the data
* @throws Exception
*/
public synchronized ResultSet getResultSet (String t, String data, String cond, String timeout) throws Exception {
StringBuffer tsb = new StringBuffer ("select ").append (data);
tsb.append (" FROM ").append (t);
tsb.append (" WHERE ").append (cond);
if (read == null) initRead ();
if (timeout != null) {
int tout = Integer.parseInt(timeout, 10);
read.setQueryTimeout(tout);
}
ResultSet rs = read.executeQuery (tsb.toString ());
if (Server.checkLogLvl (Server.MSG_AUTH, Server.LVL_VERY_VERBOSE)) {
tsb.insert (0, "PoolElement.getResultSet: returning result for statement\r\n");
Server.log (tsb.toString (), Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
}
sCnt++;
checkWarnings (read);
isActive = false;
return rs;
}
/**
* close the read-cursor
*/
public synchronized void closeRead () {
try {
read.close ();
read = null;
} catch (SQLException se) {
Server.debug ("PoolElement.closeRead:", se, Server.MSG_ERROR, Server.LVL_MINOR);
}
}
/**
* close the read-cursor
*/
public synchronized void closeWrite () {
try {
write.close ();
write = null;
} catch (SQLException se) {
Server.debug ("PoolElement.closeWrite:", se, Server.MSG_ERROR, Server.LVL_MINOR);
}
}
/**
* commit changes
* @throws Exception
*/
public synchronized void commit () throws Exception {
con.commit ();
write.close ();
write = null;
isActive = false;
}
/**
* rollback changes
* @throws Exception
*/
public synchronized void rollback () throws Exception {
con.rollback ();
write.close ();
write = null;
isActive = false;
}
/**
* cause the datarecords to be updated
* @param t the table to update
* @param data the data update
* @param cond the condition to match
* @return the number of updated records
* @throws Exception
*/
public synchronized int setData (String t, String data, String cond, String timeout) throws Exception {
StringBuffer tsb = new StringBuffer ("update ").append (t).append (" set ");
tsb.append (data);
tsb.append (" where ");
tsb.append (cond);
if (write == null) initWrite ();
if (timeout != null) {
int tout = Integer.parseInt(timeout, 10);
read.setQueryTimeout(tout);
}
int count = write.executeUpdate (tsb.toString ());
if (Server.checkLogLvl (Server.MSG_AUTH, Server.LVL_VERY_VERBOSE)) {
tsb.insert (0, "PoolElement.setData: Executed update\r\n");
Server.log (tsb.toString (), Server.MSG_AUTH, Server.LVL_VERY_VERBOSE);
}
checkWarnings (write);
return count;
}
/**
* check if this PoolElement is active
* @return true if active, false if not
*/
public synchronized boolean isActive () {
return isActive;
}
/**
* checks if this PoolElement is valid
* @return true if this element is valid, false if not
*/
synchronized boolean isValid () {
if (con == null) return false;
try {
if (con.isClosed ()) return false;
if (read == null) {
initRead ();
if (read == null) return false;
}
if (write == null) {
initWrite ();
if (write == null) return false;
}
if (sCnt > statements ||
(validUntil < System.currentTimeMillis () && validUntil != 0) ||
con.isClosed ()) {
cleanup ();
return false;
}
} catch (Exception e) {
Server.debug ("error during db-connection-cleanup", e, Server.MSG_ERROR, Server.LVL_MAJOR);
con = null;
read = write = null;
return false;
}
return true;
}
/**
* causes this PoolElement to close all open cursors and the connection to it's jdbc-source
* @throws Exception
*/
public synchronized void cleanup () throws Exception {
if (read != null) read.close ();
read = null;
if (write != null) write.close ();
write = null;
if (con != null && !con.isClosed ()) con.close ();
con = null;
Server.log ("PoolElement.cleanup: Cleanup done", Server.MSG_STATE, Server.LVL_VERBOSE);
}
public synchronized void checkWarnings(Statement s) throws Exception {
SQLWarning sqlW = s.getWarnings();
while (sqlW != null) {
StringBuffer sb = new StringBuffer("PoolElement.getResultSet: Encountered SQLWarning: ");
sb.append (sqlW.getErrorCode());
sb.append (": ");
sb.append (sqlW.getCause());
Server.log (sb.toString (), Server.MSG_ERROR, Server.LVL_MAJOR);
sqlW = sqlW.getNextWarning();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -