Expresso 5-6

com.jcorporate.expresso.core.db
Class SimpleDataSource

java.lang.Object
  extended bycom.jcorporate.expresso.core.db.SimpleDataSource
All Implemented Interfaces:
javax.sql.DataSource

public class SimpleDataSource
extends Object
implements javax.sql.DataSource

DataConnectionPool is an implementation of the javax.sql.DataSource. Normally you would get an instance of the DataConnectionPool using DBConnectionPool.getDataSource(String)

The reason for this class is that some libraries that can access a database often take a DataSource as their way of connecting to the database. Unfortunately, Expresso's DBConnectionPool's API is incompatible with the javax.sql.DataSource API. So this class wraps the DBConnectionPool with the DataSource interface so that libraries that require DataSource and Connection interfaces can operate within Expresso's database connection management capabilities.

Typical Usage:

javax.sql.DataSource dataSource = DBConnectionPool.getDataSource("default");
java.sql.Connection connection = dataSource.getConnection();
...
connection.close();

Author:
Michael Rimov
See Also:
DBConnectionPool, DBConnection

Constructor Summary
SimpleDataSource(DBConnectionPool newPoolInstance)
          Wrapping constructor.
 
Method Summary
 Connection getConnection()
          Retreive a java.sql.Connection for this data source.
 Connection getConnection(String username, String password)
          [Unused] Normally would retrieve a connection with the given username and password.
protected  DBConnectionPool getConnectionPool()
          Helper method to retrieve the instance of the connectionPool we're attached to
 int getLoginTimeout()
          Retrieve the login timeout [unimplemented]
 PrintWriter getLogWriter()
          Retrieve the current logWriter.
 void setLoginTimeout(int seconds)
          Set the login timeout.
 void setLogWriter(PrintWriter out)
          Sets the log writer and thus turns on logging
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleDataSource

public SimpleDataSource(DBConnectionPool newPoolInstance)
Wrapping constructor. Takes a DBConnectionPool instance and wraps it for the DataSource implementation

Parameters:
newPoolInstance - the instance of the DBConnectionPool
Method Detail

getConnectionPool

protected DBConnectionPool getConnectionPool()
Helper method to retrieve the instance of the connectionPool we're attached to

Returns:
DBConnectionPool instance

getConnection

public Connection getConnection()
                         throws SQLException
Retreive a java.sql.Connection for this data source. It does not attempt to retrieve pooled connections.

Specified by:
getConnection in interface javax.sql.DataSource
Returns:
java.sql.Connection
Throws:
SQLException - upon error getting the connection

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
[Unused]

Normally would retrieve a connection with the given username and password. However, that is not possible with the Expresso's DBConnectionPool which manages the default username and password as given in the configuration file. So this implementation logs a warning about this method and falls back to getConnection()

Specified by:
getConnection in interface javax.sql.DataSource
Parameters:
username - the user name for the connection
password - the password for the connection
Returns:
java.sql.Connection implementation
Throws:
SQLException - upon connection error
See Also:
getConnection()

getLogWriter

public PrintWriter getLogWriter()
                         throws SQLException
Retrieve the current logWriter. Default is null

Specified by:
getLogWriter in interface javax.sql.DataSource
Returns:
java.io.PrintWriter for the current log writer
Throws:
SQLException - upon error

setLogWriter

public void setLogWriter(PrintWriter out)
                  throws SQLException
Sets the log writer and thus turns on logging

Specified by:
setLogWriter in interface javax.sql.DataSource
Parameters:
out - the new print writer to log getConnection() calls to.
Throws:
SQLException - upon error

setLoginTimeout

public void setLoginTimeout(int seconds)
                     throws SQLException
Set the login timeout. [unimplemented]

Specified by:
setLoginTimeout in interface javax.sql.DataSource
Parameters:
seconds - the number of seconds to wait before logins timeout
Throws:
SQLException - upon error

getLoginTimeout

public int getLoginTimeout()
                    throws SQLException
Retrieve the login timeout [unimplemented]

Specified by:
getLoginTimeout in interface javax.sql.DataSource
Returns:
number of seconds before login timeout occurs (zero)
Throws:
SQLException - upon error

Expresso 5-6

Please see www.jcorporate.com for information about new Expresso releases.