Now validating connections before using them.

This commit is contained in:
Robin Kupper
2011-04-11 01:03:53 +02:00
parent 7a34fd1243
commit 40ff71e3b6
3 changed files with 23 additions and 19 deletions

View File

@@ -15,7 +15,6 @@ public class Consumer implements Runnable
{
private LinkedBlockingQueue<BlockRow> bqueue = new LinkedBlockingQueue<BlockRow>();
private HashSet<Integer> hiddenplayers = new HashSet<Integer>();
private Connection conn = null;
private LogBlock logblock;
Consumer (LogBlock logblock) {
@@ -68,13 +67,8 @@ public class Consumer implements Runnable
}
}
public void run() {
try {
if (conn == null || conn.isClosed())
conn = logblock.pool.getConnection();
} catch (SQLException ex) {
LogBlock.log.severe("[LogBlock Consumer] Can't get a connection");
}
public synchronized void run() {
Connection conn = logblock.pool.getConnection();
if (conn == null)
return;
Statement state = null;

View File

@@ -50,9 +50,9 @@ import de.diddiz.util.Download;
public class LogBlock extends JavaPlugin
{
static Logger log;
static Config config;
ConnectionPool pool;
public static Logger log;
public static Config config;
public ConnectionPool pool;
private Consumer consumer = null;
@Override

View File

@@ -36,6 +36,9 @@ import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import java.util.logging.Level;
import de.diddiz.LogBlock.LogBlock;
public class ConnectionPool implements Driver {
public static final String URL_PREFIX = "jdbc:jdc:";
@@ -58,7 +61,8 @@ public class ConnectionPool implements Driver {
public Connection getConnection() {
try {
return pool.getConnection();
} catch (SQLException e) {
} catch (SQLException ex) {
LogBlock.log.log(Level.SEVERE, "[LogBlock ConnectionPool] Error while fetching connection", ex);
return null;
}
}
@@ -83,7 +87,8 @@ public class ConnectionPool implements Driver {
return false;
}
private class JDCConnection implements Connection {
private class JDCConnection implements Connection
{
private ConnectionService pool;
private Connection conn;
private boolean inuse;
@@ -355,7 +360,8 @@ public class ConnectionPool implements Driver {
}
}
public class ConnectionService {
public class ConnectionService
{
private Vector<JDCConnection> connections;
private String url, user, password;
final private long timeout = 60000;
@@ -397,11 +403,14 @@ public class ConnectionPool implements Driver {
JDCConnection c;
for (int i = 0; i < connections.size(); i++) {
c = connections.elementAt(i);
if (c.lease())
return c;
if (c.lease()) {
if (c.validate())
return c.getConnection();
else
removeConnection(c);
}
}
Connection conn = DriverManager.getConnection(url, user, password);
c = new JDCConnection(conn, this);
c = new JDCConnection(DriverManager.getConnection(url, user, password), this);
c.lease();
connections.addElement(c);
return c.getConnection();
@@ -412,7 +421,8 @@ public class ConnectionPool implements Driver {
}
}
class ConnectionReaper extends Thread {
class ConnectionReaper extends Thread
{
private ConnectionService pool;
private final long delay = 300000;