Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions org/postgresql/Driver.java.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class Driver implements java.sql.Driver

private static final Logger logger = new Logger();
private static boolean logLevelSet = false;
private static Timer cancelTimer = new Timer(true);

static
{
Expand Down Expand Up @@ -762,15 +761,4 @@ public class Driver implements java.sql.Driver
{
throw notImplemented(this.getClass(), "getParentLogger()");
}

public static void purgeTimerTasks()
{
if ( cancelTimer != null ) cancelTimer.purge();
}

public static void addTimerTask(TimerTask timerTask, long milliSeconds)
{
cancelTimer.schedule( timerTask, milliSeconds );
}

}
12 changes: 12 additions & 0 deletions org/postgresql/PGConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package org.postgresql;

import java.sql.*;
import java.util.TimerTask;

import org.postgresql.copy.CopyManager;
import org.postgresql.fastpath.Fastpath;
import org.postgresql.largeobject.LargeObjectManager;
Expand Down Expand Up @@ -116,5 +118,15 @@ public void addDataType(String type, Class klass)
* @return PID of backend server process.
*/
public int getBackendPID();

/**
* Adds the timer task to the {@link org.postgresql.util.SharedTimer} used driver-wide.
* This method exists on the connection object because the lifetime of the shared timer is governed by existence
* of connections.
*
* @param timerTask the task to add
* @param milliSeconds the delay before the task is executed
*/
public void addTimerTask(TimerTask timerTask, long milliSeconds);
}

9 changes: 9 additions & 0 deletions org/postgresql/jdbc2/AbstractJdbc2Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public abstract class AbstractJdbc2Connection implements BaseConnection
/** Set of oids that use binary transfer when receiving from server. */
private Set<Integer> useBinaryReceiveForOids;

private SharedTimer.Loan timerLoan = SharedTimer.loan();

public abstract DatabaseMetaData getMetaData() throws SQLException;

//
Expand Down Expand Up @@ -672,6 +674,7 @@ public void close()
{
protoConnection.close();
openStackTrace = null;
timerLoan.release();
}

/*
Expand Down Expand Up @@ -1324,4 +1327,10 @@ protected void abort()
{
protoConnection.abort();
}

@Override
public void addTimerTask(TimerTask timerTask, long milliSeconds)
{
timerLoan.timer().schedule(timerTask, milliSeconds);
}
}
5 changes: 3 additions & 2 deletions org/postgresql/jdbc2/AbstractJdbc2Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.postgresql.util.PSQLState;
import org.postgresql.util.PGobject;
import org.postgresql.util.GT;
import org.postgresql.util.SharedTimer;

/**
* This class defines methods of the jdbc2 specification.
Expand Down Expand Up @@ -3439,7 +3440,7 @@ public void run()
}
};

Driver.addTimerTask( cancelTimer, timeout * 1000);
getPGConnection().addTimerTask(cancelTimer, timeout * 1000);
}

private synchronized void killTimer()
Expand All @@ -3448,7 +3449,7 @@ private synchronized void killTimer()
{
cancelTimer.cancel();
cancelTimer = null;
Driver.purgeTimerTasks();
SharedTimer.purge();
}

}
Expand Down
3 changes: 3 additions & 0 deletions org/postgresql/test/TestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Properties;

import org.postgresql.jdbc2.AbstractJdbc2Connection;
import org.postgresql.util.SharedTimer;

/**
* Utility class for JDBC tests
Expand Down Expand Up @@ -222,6 +223,8 @@ public static void closeDB(Connection con) throws SQLException
{
if (con != null)
con.close();

assert SharedTimer.getLoanCount() == 0 : "Some connections remain open after the test.";
}

/*
Expand Down
101 changes: 101 additions & 0 deletions org/postgresql/util/SharedTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* -------------------------------------------------------------------------
*
* Copyright (c) 2003-2014, PostgreSQL Global Development Group
*
*
* -------------------------------------------------------------------------
*/

package org.postgresql.util;

import java.util.Timer;

/**
* @author Lukas Krejci
*/
public final class SharedTimer
{
private static Timer timer;
private static int loanCount;

// no instances of this class
private SharedTimer() {

}

public static synchronized Loan loan()
{
if (timer == null)
{
timer = new Timer("Postgresql JDBC shared statement cancellation timer", true);
}

loanCount++;

return new Loan();
}

/**
* @see java.util.Timer#purge()
*/
public static int purge()
{
return timer == null ? 0 : timer.purge();
}

/**
* Just for testing purposes.
*/
public static synchronized int getLoanCount()
{
return loanCount;
}

private static synchronized void release()
{
loanCount--;

// clean out cancelled tasks
timer.purge();

if (loanCount == 0)
{
timer.cancel();
timer = null;
}
}

/**
* A loan of a shared timer.
*/
public static final class Loan
{
private boolean returned;

private Loan() {

}

/**
* @return the loaned timer. Don't keep a reference to the timer itself because it might be cancelled if this
* loan object is {@link #release()}ed.
*/
public Timer timer()
{
return returned ? null : timer;
}

/**
* Releases the loan making it no longer usable.
*/
public void release()
{
if (!returned)
{
returned = true;
SharedTimer.release();
}
}
}
}