org.springframework.jdbc.datasource
Class DriverManagerDataSource

java.lang.Object
  extended byorg.springframework.jdbc.datasource.AbstractDataSource
      extended byorg.springframework.jdbc.datasource.DriverManagerDataSource
All Implemented Interfaces:
DataSource, SmartDataSource
Direct Known Subclasses:
SingleConnectionDataSource

public class DriverManagerDataSource
extends AbstractDataSource
implements SmartDataSource

Implementation of SmartDataSource that configures a plain old JDBC Driver via bean properties, and returns a new Connection every time.

Useful for test or standalone environments outside of a J2EE container, either as a DataSource bean in a respective ApplicationContext, or in conjunction with a simple JNDI environment. Pool-assuming Connection.close() calls will simply close the connection, so any DataSource-aware persistence code should work.

In a J2EE container, it is recommended to use a JNDI DataSource provided by the container. Such a DataSource can be exported as a DataSource bean in an ApplicationContext via JndiObjectFactoryBean, for seamless switching to and from a local DataSource bean like this class.

If you need a "real" connection pool outside of a J2EE container, consider Apache's Jakarta Commons DBCP. Its BasicDataSource is a full connection pool bean, supporting the same basic properties as this class plus specific settings. It can be used as a replacement for an instance of this class just by changing the class name of the bean definition to "org.apache.commons.dbcp.BasicDataSource".

Since:
14.03.2003
Author:
Juergen Hoeller
See Also:
JndiObjectFactoryBean, SimpleNamingContextBuilder, BasicDataSource

Field Summary
 
Fields inherited from class org.springframework.jdbc.datasource.AbstractDataSource
logger
 
Constructor Summary
DriverManagerDataSource()
          Constructor for bean-style configuration.
DriverManagerDataSource(String driverClassName, String url, String username, String password)
          Create a new SingleConnectionDataSource with the given standard DriverManager parameters.
 
Method Summary
 Connection getConnection()
           
 Connection getConnection(String username, String password)
           
protected  Connection getConnectionFromDriverManager()
          Get a new Connection from DriverManager, with the connection properties of this DataSource.
protected  Connection getConnectionFromDriverManager(String url, String username, String password)
          Getting a connection using the nasty static from DriverManager is extracted into a protected method to allow for easy unit testing.
 String getDriverClassName()
           
 String getPassword()
           
 String getUrl()
           
 String getUsername()
           
 void setDriverClassName(String driverClassName)
           
 void setPassword(String password)
           
 void setUrl(String url)
           
 void setUsername(String username)
           
 boolean shouldClose(Connection con)
          This DataSource returns a new connection every time: Close it when returning one to the "pool".
 
Methods inherited from class org.springframework.jdbc.datasource.AbstractDataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.sql.DataSource
getLoginTimeout, getLogWriter, setLoginTimeout, setLogWriter
 

Constructor Detail

DriverManagerDataSource

public DriverManagerDataSource()
Constructor for bean-style configuration.


DriverManagerDataSource

public DriverManagerDataSource(String driverClassName,
                               String url,
                               String username,
                               String password)
                        throws CannotGetJdbcConnectionException
Create a new SingleConnectionDataSource with the given standard DriverManager parameters.

Method Detail

setDriverClassName

public void setDriverClassName(String driverClassName)
                        throws CannotGetJdbcConnectionException
Throws:
CannotGetJdbcConnectionException

getDriverClassName

public String getDriverClassName()

setUrl

public void setUrl(String url)

getUrl

public String getUrl()

setUsername

public void setUsername(String username)

getUsername

public String getUsername()

setPassword

public void setPassword(String password)

getPassword

public String getPassword()

shouldClose

public boolean shouldClose(Connection con)
This DataSource returns a new connection every time: Close it when returning one to the "pool".

Specified by:
shouldClose in interface SmartDataSource
Parameters:
con - connection, which should have been obtained from this data source, to check closure status of
Returns:
whether the given connection should be closed

getConnection

public Connection getConnection()
                         throws SQLException
Specified by:
getConnection in interface DataSource
Throws:
SQLException

getConnection

public Connection getConnection(String username,
                                String password)
                         throws SQLException
Specified by:
getConnection in interface DataSource
Throws:
SQLException

getConnectionFromDriverManager

protected Connection getConnectionFromDriverManager()
                                             throws SQLException
Get a new Connection from DriverManager, with the connection properties of this DataSource.

Throws:
SQLException

getConnectionFromDriverManager

protected Connection getConnectionFromDriverManager(String url,
                                                    String username,
                                                    String password)
                                             throws SQLException
Getting a connection using the nasty static from DriverManager is extracted into a protected method to allow for easy unit testing.

Throws:
SQLException


Copyright (C) 2003-2004 The Spring Framework Project.