public class DataSourceConnectionProvider extends Object implements ConnectionProvider
DataSource
instances (one for read-only operations
and one for read-write operations). Note: if transactions are going to be used, the data sources
must be pooled data sources, otherwise bad things will happen.Constructor and Description |
---|
DataSourceConnectionProvider(String url,
DataSource readSource,
DataSource writeSource)
Creates a connection provider that will obtain connections from the supplied read-only and
read-write sources.
|
Modifier and Type | Method and Description |
---|---|
void |
connectionFailed(String ident,
boolean readOnly,
Connection conn,
SQLException error)
Called by the repository if a failure occurred on the connection.
|
Connection |
getConnection(String ident,
boolean readOnly)
Obtains a database connection based on the supplied database identifier.
|
Connection |
getTxConnection(String ident)
Returns a connection that can be used in a transaction.
|
String |
getURL(String ident)
Returns the URL associated with this database identifier.
|
void |
releaseConnection(String ident,
boolean readOnly,
Connection conn)
Releases a database connection when it is no longer needed by the repository.
|
void |
releaseTxConnection(String ident,
Connection conn)
Releases a connection obtained by
ConnectionProvider.getTxConnection(java.lang.String) . |
void |
shutdown()
Shuts down this connection provider, closing all connections currently in the pool.
|
void |
txConnectionFailed(String ident,
Connection conn,
SQLException error)
Reports failure of a connection obtained by
ConnectionProvider.getTxConnection(java.lang.String) . |
public DataSourceConnectionProvider(String url, DataSource readSource, DataSource writeSource)
ident
mechanism is not used by this provider.
Responsibility for the lifecycle of the supplied datasources is left to the caller and is
not managed by the connection provider (shutdown()
does nothing).url
- a URL prefix that can be used by the DatabaseLiaison
to identify the type
of database being accessed by the supplied data sources, this should be one of "jdbc:mysql"
or "jdbc:postgresql" as those are the only two types of database currently supported.public Connection getConnection(String ident, boolean readOnly) throws PersistenceException
ConnectionProvider
ConnectionProvider.releaseConnection(java.lang.String, boolean, java.sql.Connection)
or ConnectionProvider.connectionFailed(java.lang.String, boolean, java.sql.Connection, java.sql.SQLException)
depending on the circumstances of the release. close()
will not be called on the connection. It is up to the connection provider to close
the connection when it is released if appropriate.getConnection
in interface ConnectionProvider
ident
- the database connection identifier.readOnly
- whether or not the connection may be to a read-only mirror of the
repository.PersistenceException
- thrown if a problem occurs trying to open the requested
connection.public void releaseConnection(String ident, boolean readOnly, Connection conn)
ConnectionProvider
releaseConnection
in interface ConnectionProvider
ident
- the database identifier used when obtaining this connection.readOnly
- the same value that was passed to ConnectionProvider.getConnection(java.lang.String, boolean)
to obtain this
connection.conn
- the connection to release (back into the pool or to be closed if pooling is not
going on).public void connectionFailed(String ident, boolean readOnly, Connection conn, SQLException error)
ConnectionProvider
getConnection
will
return a freshly established connection to the database.connectionFailed
in interface ConnectionProvider
ident
- the database identifier used when obtaining this connection.readOnly
- the same value that was passed to ConnectionProvider.getConnection(java.lang.String, boolean)
to obtain this
connection.conn
- the connection that failed.error
- the error thrown by the connection (which may be used to determine if the
connection should be closed or can be reused).public Connection getTxConnection(String ident) throws PersistenceException
ConnectionProvider
ConnectionProvider.getConnection(java.lang.String, boolean)
. Note:
transaction connections are never read-only. What would be the point?getTxConnection
in interface ConnectionProvider
PersistenceException
public void releaseTxConnection(String ident, Connection conn)
ConnectionProvider
ConnectionProvider.getTxConnection(java.lang.String)
. See ConnectionProvider.releaseConnection(java.lang.String, boolean, java.sql.Connection)
.releaseTxConnection
in interface ConnectionProvider
public void txConnectionFailed(String ident, Connection conn, SQLException error)
ConnectionProvider
ConnectionProvider.getTxConnection(java.lang.String)
. See ConnectionProvider.connectionFailed(java.lang.String, boolean, java.sql.Connection, java.sql.SQLException)
.txConnectionFailed
in interface ConnectionProvider
public String getURL(String ident)
ConnectionProvider
ConnectionProvider.getConnection(java.lang.String, boolean)
were called.getURL
in interface ConnectionProvider
public void shutdown()
ConnectionProvider
ConnectionProvider.releaseConnection(java.lang.String, boolean, java.sql.Connection)
.shutdown
in interface ConnectionProvider
Copyright © 2015. All rights reserved.