org.springframework.jdbc.datasource
Class DriverManagerDataSource

org.springframework.jdbc.datasource.AbstractDataSource
  |
  +--org.springframework.jdbc.datasource.DriverManagerDataSource
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 container, consider Apache's Jakarta Commons DBCP. Its BasicDataSource is a full connection pool bean, supporting the same basic properties as this class + specific settings. It can be used as a replacement for an instance of this class just by exchanging the class name of the bean definition to "org.apache.commons.dbcp.BasicDataSource".

Author:
Juergen Hoeller
Version: $Id: DriverManagerDataSource.java,v 1.4 2003/11/13 11:11:06 jhoeller Exp $
Since: 14.03.2003
See Also: SimpleNamingContextBuilder, JndiObjectFactoryBean

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
 ConnectiongetConnection()
           
 ConnectiongetConnection(String username, String password)
           
 StringgetDriverClassName()
           
 StringgetPassword()
           
 StringgetUrl()
           
 StringgetUsername()
           
 voidsetDriverClassName(String driverClassName)
           
 voidsetPassword(String password)
           
 voidsetUrl(String url)
           
 voidsetUsername(String username)
           
 booleanshouldClose(Connection conn)
          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

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

getConnection

public Connection getConnection()
throws java.sql.SQLException

getConnection

public Connection getConnection(String username, String password)
throws java.sql.SQLException

getDriverClassName

public String getDriverClassName()

getPassword

public String getPassword()

getUrl

public String getUrl()

getUsername

public String getUsername()

setDriverClassName

public void setDriverClassName(String driverClassName)
throws CannotGetJdbcConnectionException

setPassword

public void setPassword(String password)

setUrl

public void setUrl(String url)

setUsername

public void setUsername(String username)

shouldClose

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

Association Links

to Class java.lang.String

to Class java.lang.String

to Class java.lang.String

to Class java.lang.String