public class SimpleDbConnectionFactory extends Object implements DbConnectionFactory
DbConnectionFactory
that creates a
new connection for every request and does no caching or connection sharing.
Note that no real connection pooling is occurring within this class. This factory will be an inefficient connection management scheme for most real-world applications. It was designed only for development/debugging purposes and for applications that are very infrequently used. In those cases, caching idle database connections for long periods of time can be wasteful, and this pooling scheme would be appropriate.
This manager simply initializes the JDBC driver and then stores the database credential information (if any). Then when a connection is requested, a new connection is made with the stored credentials (if any).
Constructor and Description |
---|
SimpleDbConnectionFactory() |
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Clear all database URL and credential information so no more connections
can be requested.
|
Connection |
getConnection()
Create a new connection for the given database URL.
|
void |
init(String dbUrl,
String dbDriver,
String username,
String password)
Initialize a database factory with the given URL, driver classname, and
database credentials.
|
void |
releaseConnection(Connection connection)
Replace a database connection back in the pool of available connections
for its parent pool.
|
public void init(String dbUrl, String dbDriver, String username, String password) throws ClassNotFoundException
Only one init
method should be called.
Initialize a new database pool with the given database username and
password. This method will load the JDBC driver and store the given
database credentials. When a connection is requested, a new connection
will be made using the credentials.
init
in interface DbConnectionFactory
dbUrl
- the JDBC URL used to retrieve connectionsdbDriver
- a fully qualified class name for the JDBC driver that will
handle this JDBC URLusername
- the name to use to authenticate us with the databasepassword
- the credentials use to authenticate the usernameClassNotFoundException
- if any.public void destroy()
destroy
in interface DbConnectionFactory
public Connection getConnection() throws SQLException
getConnection
in interface DbConnectionFactory
Connection
object.SQLException
- if any.public void releaseConnection(Connection connection) throws SQLException
releaseConnection
in interface DbConnectionFactory
connection
- the connection to releaseSQLException
- if any.Copyright © 2016. All rights reserved.