com.j256.ormlite.jdbc
Class JdbcPooledConnectionSource

java.lang.Object
  extended by com.j256.ormlite.support.BaseConnectionSource
      extended by com.j256.ormlite.jdbc.JdbcConnectionSource
          extended by com.j256.ormlite.jdbc.JdbcPooledConnectionSource
All Implemented Interfaces:
com.j256.ormlite.support.ConnectionSource

public class JdbcPooledConnectionSource
extends JdbcConnectionSource
implements com.j256.ormlite.support.ConnectionSource

Implementation of the ConnectionSource interface that supports basic pooled connections. New connections are created on demand only if there are no dormant connections otherwise released connections will be reused. This class is reentrant and can handle requests from multiple threads.

NOTE: If you are using the Spring type wiring in Java, initialize() should be called after all of the set methods. In Spring XML, init-method="initialize" should be used.

NOTE: This class spawns a thread to test the pooled connections that are in the free-list as a keep-alive mechanism. It will test any dormant connections every so often to see if they are still valid. If this is not the behavior that you want then call setCheckConnectionsEveryMillis(long) with 0 to disable the thread. You can also call setTestBeforeGet(boolean) and set it to true to test the connection before it is handed back to you.

Author:
graywatson

Field Summary
 
Fields inherited from class com.j256.ormlite.jdbc.JdbcConnectionSource
databaseType, initialized
 
Constructor Summary
JdbcPooledConnectionSource()
           
JdbcPooledConnectionSource(String url)
           
JdbcPooledConnectionSource(String url, com.j256.ormlite.db.DatabaseType databaseType)
           
JdbcPooledConnectionSource(String url, String username, String password)
           
JdbcPooledConnectionSource(String url, String username, String password, com.j256.ormlite.db.DatabaseType databaseType)
           
 
Method Summary
 void clearSpecialConnection(com.j256.ormlite.support.DatabaseConnection connection)
           
 void close()
           
 int getCloseCount()
          Return the approximate number of connections closed over the life of the pool.
 int getCurrentConnectionsFree()
          Return the number of currently freed connections in the free list.
 int getCurrentConnectionsManaged()
          Return the number of current connections that we are tracking.
 int getMaxConnectionsEverUsed()
          Return the approximate maximum number of connections in use at one time.
 int getOpenCount()
          Return the approximate number of connections opened over the life of the pool.
 com.j256.ormlite.support.DatabaseConnection getReadOnlyConnection()
           
 com.j256.ormlite.support.DatabaseConnection getReadWriteConnection()
           
 int getReleaseCount()
          Return the approximate number of connections released over the life of the pool.
 int getTestLoopCount()
          Mostly for testing purposes to see how many times our test loop ran.
 void initialize()
          Initialize the class after the setters have been called.
 boolean isOpen()
           
 void releaseConnection(com.j256.ormlite.support.DatabaseConnection connection)
           
 boolean saveSpecialConnection(com.j256.ormlite.support.DatabaseConnection connection)
           
 void setCheckConnectionsEveryMillis(long checkConnectionsEveryMillis)
          There is an internal thread which checks each of the database connections as a keep-alive mechanism.
 void setMaxConnectionAgeMillis(long maxConnectionAgeMillis)
          Set the number of milliseconds that a connection can stay open before being closed.
 void setMaxConnectionsFree(int maxConnectionsFree)
          Set the number of connections that can be unused in the available list.
 void setTestBeforeGet(boolean testBeforeGetFromPool)
           
 void setUsesTransactions(boolean usesTransactions)
          Deprecated. No longer supported and can be removed.
 
Methods inherited from class com.j256.ormlite.jdbc.JdbcConnectionSource
closeQuietly, getDatabaseType, getUrl, makeConnection, setDatabaseType, setPassword, setUrl, setUsername
 
Methods inherited from class com.j256.ormlite.support.BaseConnectionSource
clearSpecial, getSavedConnection, getSpecialConnection, isSavedConnection, saveSpecial
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.j256.ormlite.support.ConnectionSource
closeQuietly, getDatabaseType, getSpecialConnection
 

Constructor Detail

JdbcPooledConnectionSource

public JdbcPooledConnectionSource()

JdbcPooledConnectionSource

public JdbcPooledConnectionSource(String url)
                           throws SQLException
Throws:
SQLException

JdbcPooledConnectionSource

public JdbcPooledConnectionSource(String url,
                                  com.j256.ormlite.db.DatabaseType databaseType)
                           throws SQLException
Throws:
SQLException

JdbcPooledConnectionSource

public JdbcPooledConnectionSource(String url,
                                  String username,
                                  String password)
                           throws SQLException
Throws:
SQLException

JdbcPooledConnectionSource

public JdbcPooledConnectionSource(String url,
                                  String username,
                                  String password,
                                  com.j256.ormlite.db.DatabaseType databaseType)
                           throws SQLException
Throws:
SQLException
Method Detail

initialize

public void initialize()
                throws SQLException
Description copied from class: JdbcConnectionSource
Initialize the class after the setters have been called. If you are using the no-arg constructor and Spring type wiring, this should be called after all of the set methods.

Overrides:
initialize in class JdbcConnectionSource
Throws:
SQLException - If the driver associated with the database URL is not found in the classpath.

close

public void close()
           throws SQLException
Specified by:
close in interface com.j256.ormlite.support.ConnectionSource
Overrides:
close in class JdbcConnectionSource
Throws:
SQLException

getReadOnlyConnection

public com.j256.ormlite.support.DatabaseConnection getReadOnlyConnection()
                                                                  throws SQLException
Specified by:
getReadOnlyConnection in interface com.j256.ormlite.support.ConnectionSource
Overrides:
getReadOnlyConnection in class JdbcConnectionSource
Throws:
SQLException

getReadWriteConnection

public com.j256.ormlite.support.DatabaseConnection getReadWriteConnection()
                                                                   throws SQLException
Specified by:
getReadWriteConnection in interface com.j256.ormlite.support.ConnectionSource
Overrides:
getReadWriteConnection in class JdbcConnectionSource
Throws:
SQLException

releaseConnection

public void releaseConnection(com.j256.ormlite.support.DatabaseConnection connection)
                       throws SQLException
Specified by:
releaseConnection in interface com.j256.ormlite.support.ConnectionSource
Overrides:
releaseConnection in class JdbcConnectionSource
Throws:
SQLException

saveSpecialConnection

public boolean saveSpecialConnection(com.j256.ormlite.support.DatabaseConnection connection)
                              throws SQLException
Specified by:
saveSpecialConnection in interface com.j256.ormlite.support.ConnectionSource
Overrides:
saveSpecialConnection in class JdbcConnectionSource
Throws:
SQLException

clearSpecialConnection

public void clearSpecialConnection(com.j256.ormlite.support.DatabaseConnection connection)
Specified by:
clearSpecialConnection in interface com.j256.ormlite.support.ConnectionSource
Overrides:
clearSpecialConnection in class JdbcConnectionSource

isOpen

public boolean isOpen()
Specified by:
isOpen in interface com.j256.ormlite.support.ConnectionSource
Overrides:
isOpen in class JdbcConnectionSource

setUsesTransactions

@Deprecated
public void setUsesTransactions(boolean usesTransactions)
Deprecated. No longer supported and can be removed.


setMaxConnectionsFree

public void setMaxConnectionsFree(int maxConnectionsFree)
Set the number of connections that can be unused in the available list.


setMaxConnectionAgeMillis

public void setMaxConnectionAgeMillis(long maxConnectionAgeMillis)
Set the number of milliseconds that a connection can stay open before being closed. Set to Long.MAX_VALUE to have the connections never expire.


getOpenCount

public int getOpenCount()
Return the approximate number of connections opened over the life of the pool.


getReleaseCount

public int getReleaseCount()
Return the approximate number of connections released over the life of the pool.


getCloseCount

public int getCloseCount()
Return the approximate number of connections closed over the life of the pool.


getMaxConnectionsEverUsed

public int getMaxConnectionsEverUsed()
Return the approximate maximum number of connections in use at one time.


getCurrentConnectionsFree

public int getCurrentConnectionsFree()
Return the number of currently freed connections in the free list.


getCurrentConnectionsManaged

public int getCurrentConnectionsManaged()
Return the number of current connections that we are tracking.


setCheckConnectionsEveryMillis

public void setCheckConnectionsEveryMillis(long checkConnectionsEveryMillis)
There is an internal thread which checks each of the database connections as a keep-alive mechanism. This set the number of milliseconds it sleeps between checks -- default is 30000. To disable the checking thread, set this to 0 before you start using the connection source.


setTestBeforeGet

public void setTestBeforeGet(boolean testBeforeGetFromPool)

getTestLoopCount

public int getTestLoopCount()
Mostly for testing purposes to see how many times our test loop ran.



This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.