Awake SQL v1.2.1

org.awakefw.commons.api.server
Class AwakeDataSource

java.lang.Object
  extended by org.awakefw.commons.api.server.AwakeDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource

public class AwakeDataSource
extends Object
implements DataSource

Implementation of DataSource that allows the wrapping of Awake default connection pooling system.

Notes:

1) This class will be automatically loaded by AwakeDataSourceFactory in a Java EE environment.

2) This class may also be called directly using the constructor and setting the values in order to extract a Connection from the pool.
Note that the class is not immutable because it must be a Java Bean for some Java EE servers as Caucho Resin.

Example:

 // Declare the DataSource as member to load only once the pool:
 private AwakeDataSource awakeDataSource = null;
 ...
 ...
 // Driver parameters to use:
 String driverClassName = "org.postgresql.Driver";
 String url = "jdbc:postgresql://localhost:5432/awake-example";
 String username = "user1";
 String password = "password1";
 int minConns = 2;
 int maxConns = 15;
 int maxConnTime = 1;
 int maxCheckoutSeconds = 180;
 boolean logAppend = false;
 File logfile = new File("/home/admin/log/mylog.log");
  
 // Creating the DataSource bean and populating the values:
 if (awakeDataSource == null) {
     awakeDataSource = new AwakeDataSource();
     awakeDataSource.setDriverClassName(driverClassName);
     awakeDataSource.setUrl(url);
     awakeDataSource.setUsername(username);
     awakeDataSource.setPassword(password);
     awakeDataSource.setMinConns(minConns);
     awakeDataSource.setMaxConns(maxConns);
     awakeDataSource.setMaxConnTime(maxConnTime);
     awakeDataSource.setMaxCheckoutSeconds(maxCheckoutSeconds);
     awakeDataSource.setLogAppend(logAppend);     
     awakeDataSource.setLogFile(logFile);
 }
  
 // Extracting the Connection from the connection pool:
 Connection connection = awakeDataSource.getConnection();
 
Note that after the first call to awakeDataSource.getConnection(), it is then possible to query the state of the connection pool with:

Since:
1.0
See Also:
AwakeDataSourceFactory, DefaultAwakeCommonsConfigurator

Constructor Summary
AwakeDataSource()
          This constructor is mandatory for EJB behavior.
 
Method Summary
 long getAge(Connection connnection)
          Returns the age of a Connection -- the time since it was handed out to an application.
 Connection getConnection()
          Returns a Connection to the database.
 Connection getConnection(String username, String password)
          Returns a Connection to the database.
 String getDriverClassName()
          Returns the fully qualified Java class name of your JDBC driver.
 File getLogFile()
          Returns the absolute path name for log file.
 int getLoginTimeout()
          Returns the login timeout (in seconds) for connecting to the database.
 PrintWriter getLogWriter()
          Returns the log writer being used by this data source.
 int getMaxCheckoutSeconds()
          Returns the maximum time in seconds a Connection can be checked out before being recycled.
 int getMaxConns()
          Returns the maximum number of connections in dynamic pool.
 double getMaxConnTime()
          Returns the time in days between connection resets.
 int getMinConns()
          Returns the minimum number of connections to start with.
 Logger getParentLogger()
          Method not implemented in Awake SQL v1.0.
 String getPassword()
          Returns the database password.
 int getSize()
          Returns the number of Connections in the dynamic pool.
 String getUrl()
          Returns the Connection URL.
 int getUseCount()
          Returns the number of Connections in use.
 String getUsername()
          Returns the database username.
 int idOfConnection(Connection connnection)
          Returns the local JDBC ID for a Connection.
 boolean isLogAppend()
          Returns if log is in append.
 boolean isWrapperFor(Class<?> iface)
          Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does.
 void setDriverClassName(String driverClassName)
          Sets the JDBC driver class name.
 void setLogAppend(boolean logAppend)
          Sets if the log is in append.
 void setLogFile(File logFile)
          Sets the absolute path name for log file. e.g.
 void setLoginTimeout(int loginTimeout)
          Sets the login timeout (in seconds) for connecting to the database.
 void setLogWriter(PrintWriter logWriter)
          Sets the log writer being used by this data source.
 void setMaxCheckoutSeconds(int maxCheckoutSeconds)
          Sets the maximum time in seconds a Connection can be checked out before being recycled.
 void setMaxConns(int maxConns)
          Sets the maximum number of connections in dynamic pool.
 void setMaxConnTime(double maxConnTime)
          Sets the time in days between connection resets.
 void setMinConns(int minConns)
          Sets the minimum number of connections to start with.
 void setPassword(String password)
          Sets the database password to be passed to your JDBC driver.
 void setUrl(String url)
          Sets the Connection URL to be passed to your JDBC driver
 void setUsername(String username)
          Sets the database username to be passed to your JDBC driver.
<T> T
unwrap(Class<T> iface)
          Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AwakeDataSource

public AwakeDataSource()
This constructor is mandatory for EJB behavior. Values are set with setters.

Method Detail

getAge

public long getAge(Connection connnection)
Returns the age of a Connection -- the time since it was handed out to an application.

Parameters:
connnection - the JDBC Connection

getConnection

public Connection getConnection()
                         throws SQLException
Returns a Connection to the database.

Specified by:
getConnection in interface DataSource
Throws:
SQLException - if a database access error occurs

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Returns a Connection to the database.

Specified by:
getConnection in interface DataSource
Parameters:
username - Database user on whose behalf the Connection is being made
password - The database user's password
Throws:
SQLException - if a database access error occurs

getDriverClassName

public String getDriverClassName()
Returns the fully qualified Java class name of your JDBC driver.

Returns:
the fully qualified Java class name of your JDBC driver

getLogFile

public File getLogFile()
Returns the absolute path name for log file.

Returns:
the absolute path name for log file

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Returns the login timeout (in seconds) for connecting to the database.

Specified by:
getLoginTimeout in interface CommonDataSource
Throws:
SQLException - if a database access error occurs

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Returns the log writer being used by this data source.

Specified by:
getLogWriter in interface CommonDataSource
Throws:
SQLException - if a database access error occurs

getMaxCheckoutSeconds

public int getMaxCheckoutSeconds()
Returns the maximum time in seconds a Connection can be checked out before being recycled. This value is thus the maximum duration of a SQL request.

Returns:
the maximum time in seconds a Connection can be checked out before being recycled.

getMaxConns

public int getMaxConns()
Returns the maximum number of connections in dynamic pool.

Returns:
the maximum number of connections in dynamic pool.

getMaxConnTime

public double getMaxConnTime()
Returns the time in days between connection resets.

Returns:
the time in days between connection reset

getMinConns

public int getMinConns()
Returns the minimum number of connections to start with.

Returns:
the minimum number of connections to start with

getParentLogger

public Logger getParentLogger()
                       throws SQLFeatureNotSupportedException
Method not implemented in Awake SQL v1.0.

Return the parent Logger of all the Loggers used by this data source. This should be the Logger farthest from the root Logger that is still an ancestor of all of the Loggers used by this data source. Configuring this Logger will affect all of the log messages generated by the data source. In the worst case, this may be the root Logger.

Returns:
the parent Logger for this data source
Throws:
SQLFeatureNotSupportedException - if the data source does not use java.util.logging.
Since:
1.7

getPassword

public String getPassword()
Returns the database password.

Returns:
the database password

getSize

public int getSize()
Returns the number of Connections in the dynamic pool.


getUrl

public String getUrl()
Returns the Connection URL.

Returns:
the Connection URL

getUseCount

public int getUseCount()
Returns the number of Connections in use.


getUsername

public String getUsername()
Returns the database username.

Returns:
the database username

idOfConnection

public int idOfConnection(Connection connnection)
Returns the local JDBC ID for a Connection.

Parameters:
connnection - the JDBC Connection

isLogAppend

public boolean isLogAppend()
Returns if log is in append.

Returns:
true if log is append, else false.

isWrapperFor

public boolean isWrapperFor(Class<?> iface)
                     throws SQLException
Returns true if this either implements the interface argument or is directly or indirectly a wrapper for an object that does. Returns false otherwise. If this implements the interface then return true, else if this is a wrapper then return the result of recursively calling isWrapperFor on the wrapped object. If this does not implement the interface and is not a wrapper, return false. This method should be implemented as a low-cost operation compared to unwrap so that callers can use this method to avoid expensive unwrap calls that may fail. If this method returns true then calling unwrap with the same argument should succeed.

Specified by:
isWrapperFor in interface Wrapper
Parameters:
iface - a Class defining an interface.
Returns:
true if this implements the interface or directly or indirectly wraps an object that does.
Throws:
SQLException - if an error occurs while determining whether this is a wrapper for an object with the given interface.
Since:
1.6

setDriverClassName

public void setDriverClassName(String driverClassName)
Sets the JDBC driver class name.

Parameters:
driverClassName - JDBC driver class name.

setLogAppend

public void setLogAppend(boolean logAppend)
Sets if the log is in append. Default is false.

Parameters:
logAppend - true to log in append, else false

setLogFile

public void setLogFile(File logFile)
Sets the absolute path name for log file. e.g. '/tmp/mylog.log'. Defaults to user.home/.awake/awake.sql.log.

Parameters:
logFile - absolute path name for log file.

setLoginTimeout

public void setLoginTimeout(int loginTimeout)
                     throws SQLException
Sets the login timeout (in seconds) for connecting to the database.

Specified by:
setLoginTimeout in interface CommonDataSource
Parameters:
loginTimeout - The new login timeout, or zero for no timeout
Throws:
SQLException - if a database access error occurs

setLogWriter

public void setLogWriter(PrintWriter logWriter)
                  throws SQLException
Sets the log writer being used by this data source.

Specified by:
setLogWriter in interface CommonDataSource
Parameters:
logWriter - The new log writer
Throws:
SQLException - if a database access error occurs

setMaxCheckoutSeconds

public void setMaxCheckoutSeconds(int maxCheckoutSeconds)
Sets the maximum time in seconds a Connection can be checked out before being recycled. This value is thus the maximum duration of a SQL request. Default value is 180 seconds. Zero value turns option off.

Parameters:
maxCheckoutSeconds - the maximum time in seconds a Connection can be checked out before being recycled.

setMaxConns

public void setMaxConns(int maxConns)
Sets the maximum number of connections in dynamic pool. Must be >= minConns.

Parameters:
maxConns - the maximum number of connections in dynamic pool. Must be >= minConns.

setMaxConnTime

public void setMaxConnTime(double maxConnTime)
Sets the time in days between connection resets. Defaults to 1. (Reset does a basic cleanup).

Parameters:
maxConnTime - time in days between connection resets.

setMinConns

public void setMinConns(int minConns)
Sets the minimum number of connections to start with. Must be > 0.

Parameters:
minConns - minimum number of connections to start with. Must be > 0.

setPassword

public void setPassword(String password)
Sets the database password to be passed to your JDBC driver.

Parameters:
password - the Database password to be passed to your JDBC driver.

setUrl

public void setUrl(String url)
Sets the Connection URL to be passed to your JDBC driver

Parameters:
url - the Connection URL to be passed to your JDBC driver

setUsername

public void setUsername(String username)
Sets the database username to be passed to your JDBC driver.

Parameters:
username - the Database username to be passed to your JDBC driver.

unwrap

public <T> T unwrap(Class<T> iface)
         throws SQLException
Returns an object that implements the given interface to allow access to non-standard methods, or standard methods not exposed by the proxy. If the receiver implements the interface then the result is the receiver or a proxy for the receiver. If the receiver is a wrapper and the wrapped object implements the interface then the result is the wrapped object or a proxy for the wrapped object. Otherwise return the the result of calling unwrap recursively on the wrapped object or a proxy for that result. If the receiver is not a wrapper and does not implement the interface, then an SQLException is thrown.

Specified by:
unwrap in interface Wrapper
Parameters:
iface - A Class defining an interface that the result must implement.
Returns:
an object that implements the interface. May be a proxy for the actual implementing object.
Throws:
SQLException - If no object found that implements the interface
Since:
1.6

Awake SQL v1.2.1

Awake SQL - Virtual JDBC Driver over HTTP for Android, Swing and JavaFX
Android & Desktop Edition allow easy access to majors remote SQL databases with regular JDBC calls:
DB2 - H2 - HSQLDB (HyperSQL) - Informix - Ingres - MySQL - Oracle - PostgreSQL - SQL Server - Sybase - Teradata
Copyright © 2012 Kawan Softwares SAS