public class ConnectionHelper extends Object
getConnection(String)
, that will return a
connection wrapper able to use a shared connection when used in transactional mode and setAutoCommit(false) is
called, and otherwise use a normal physical JDBC connection.
The physical connections are created from the datasource configured using the framework property "nuxeo.db.singleDataSource".
This helper is used to implement consistent resource management in a non-XA context. Several users of the shared connection can call setAutoCommit(false) then do transactional work and commit(). Only the commit() of the last user will do an actual commit on the physical connection.
Modifier and Type | Field and Description |
---|---|
static String |
EXCLUDE_DS
Property holding one ore more datasource names (comma or space separated) for whose connections the single
datasource is not used.
|
static String |
SINGLE_DS
Property holding a datasource name to use to replace all database accesses.
|
Constructor and Description |
---|
ConnectionHelper() |
Modifier and Type | Method and Description |
---|---|
static void |
clearConnectionReferences()
Clears the remaining connection references for the current thread.
|
static int |
countConnectionReferences()
Checks how many references there are to shared connections.
|
static Connection |
getConnection(String dataSourceName)
Gets a new reference to the transaction-local JDBC connection for the given dataSource.
|
static Connection |
getConnection(String dataSourceName,
boolean noSharing)
Gets a new reference to the transaction-local JDBC connection for the given dataSource.
|
static String |
getPseudoDataSourceNameForRepository(String repositoryName)
Gets the fake name we use to pass to ConnectionHelper.getConnection, in order for exclusions on these connections
to be possible.
|
static boolean |
registerSynchronization(Synchronization sync)
If sharing is in effect, registers a synchronization with the current transaction, making sure it runs before the
SharedConnectionSynchronization . |
static boolean |
registerSynchronizationLast(Synchronization sync)
If sharing is in effect, registers a synchronization with the current transaction, making sure the
Synchronization.afterCompletion(int) method runs after the SharedConnectionSynchronization . |
static Connection |
unwrap(Connection connection)
Tries to unwrap the connection to get the real physical one (returned by the original datasource).
|
static boolean |
useSingleConnection(String dataSourceName)
Checks if single transaction-local datasource mode will be used for the given datasource name.
|
public static final String SINGLE_DS
public static final String EXCLUDE_DS
public ConnectionHelper()
public static Connection unwrap(Connection connection) throws SQLException
This should only be used by code that needs to cast the connection to a driver-specific class to use driver-specific features.
SQLException
- if no actual physical connection was allocated yetpublic static boolean useSingleConnection(String dataSourceName)
true
if using a single transaction-local connection for this datasourcepublic static String getPseudoDataSourceNameForRepository(String repositoryName)
public static Connection getConnection(String dataSourceName) throws SQLException
If the passed dataSource name is in the exclusion list, null will be returned.
dataSourceName
- the datasource for which the connection is requestednull
if single datasource connection sharing is not in
effectjavax.resource.ResourceException
SQLException
public static Connection getConnection(String dataSourceName, boolean noSharing) throws SQLException
If the passed dataSource name is in the exclusion list, null will be returned.
If noSharing is requested, the connection will never come from the transaction-local and will always be newly allocated.
dataSourceName
- the datasource for which the connection is requestednoSharing
- true
if this connection must not be shared with othersnull
if single datasource connection sharing is not in
effectjavax.resource.ResourceException
SQLException
public static int countConnectionReferences()
USED IN UNIT TESTS OR FOR DEBUGGING.
public static void clearConnectionReferences()
USED IN UNIT TESTS ONLY.
public static boolean registerSynchronization(Synchronization sync) throws SystemException
SharedConnectionSynchronization
.true
SystemException
public static boolean registerSynchronizationLast(Synchronization sync) throws SystemException
Synchronization.afterCompletion(int)
method runs after the SharedConnectionSynchronization
.true
SystemException
Copyright © 2015 Nuxeo SA. All rights reserved.