org.springframework.jdbc.datasource
Class WebSphereDataSourceAdapter

java.lang.Object
  extended by org.springframework.jdbc.datasource.DelegatingDataSource
      extended by org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter
          extended by org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter
              extended by org.springframework.jdbc.datasource.WebSphereDataSourceAdapter
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource, InitializingBean

public class WebSphereDataSourceAdapter
extends IsolationLevelDataSourceAdapter

DataSource implementation that delegates all calls to a WebSphere target DataSource, typically obtained from JNDI, applying a current isolation level and/or current user credentials to every Connection obtained from it.

Uses IBM-specific API to get a JDBC Connection with a specific isolation level (and read-only flag) from a WebSphere DataSource (IBM code example). Supports the transaction-specific isolation level exposed by TransactionSynchronizationManager.getCurrentTransactionIsolationLevel(). It's also possible to specify a default isolation level, to be applied when the current Spring-managed transaction does not define a specific isolation level.

Usage example, defining the target DataSource as an inner-bean JNDI lookup (of course, you can link to any WebSphere DataSource through a bean reference):

 <bean id="myDataSource" class="org.springframework.jdbc.datasource.WebSphereDataSourceAdapter">
   <property name="targetDataSource">
     <bean class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="jndiName" value="jdbc/myds"/>
     </bean>
   </property>
 </bean>
Thanks to Ricardo Olivieri for submitting the original implementation of this approach!

Since:
2.0.3
Author:
Juergen Hoeller, Lari Hotari, Ricardo N. Olivieri
See Also:
com.ibm.websphere.rsadapter.JDBCConnectionSpec, com.ibm.websphere.rsadapter.WSDataSource#getConnection(com.ibm.websphere.rsadapter.JDBCConnectionSpec), TransactionSynchronizationManager.getCurrentTransactionIsolationLevel(), TransactionSynchronizationManager.isCurrentTransactionReadOnly()

Field Summary
protected  Log logger
           
 
Constructor Summary
WebSphereDataSourceAdapter()
          This constructor retrieves the WebSphere JDBC connection spec API, so we can get obtain specific WebSphere Connections using reflection.
 
Method Summary
 void afterPropertiesSet()
          Checks that the specified 'targetDataSource' actually is a WebSphere WSDataSource.
protected  Object createConnectionSpec(Integer isolationLevel, Boolean readOnlyFlag, String username, String password)
          Create a WebSphere JDBCConnectionSpec object for the given charateristics.
protected  Connection doGetConnection(String username, String password)
          Builds a WebSphere JDBCConnectionSpec object for the current settings and calls WSDataSource.getConnection(JDBCConnectionSpec).
 
Methods inherited from class org.springframework.jdbc.datasource.IsolationLevelDataSourceAdapter
getCurrentIsolationLevel, getCurrentReadOnlyFlag, getIsolationLevel, setIsolationLevel, setIsolationLevelName
 
Methods inherited from class org.springframework.jdbc.datasource.UserCredentialsDataSourceAdapter
getConnection, getConnection, removeCredentialsFromCurrentThread, setCredentialsForCurrentThread, setPassword, setUsername
 
Methods inherited from class org.springframework.jdbc.datasource.DelegatingDataSource
getLoginTimeout, getLogWriter, getParentLogger, getTargetDataSource, isWrapperFor, setLoginTimeout, setLogWriter, setTargetDataSource, unwrap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

WebSphereDataSourceAdapter

public WebSphereDataSourceAdapter()
This constructor retrieves the WebSphere JDBC connection spec API, so we can get obtain specific WebSphere Connections using reflection.

Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Checks that the specified 'targetDataSource' actually is a WebSphere WSDataSource.

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class DelegatingDataSource

doGetConnection

protected Connection doGetConnection(String username,
                                     String password)
                              throws SQLException
Builds a WebSphere JDBCConnectionSpec object for the current settings and calls WSDataSource.getConnection(JDBCConnectionSpec).

Overrides:
doGetConnection in class IsolationLevelDataSourceAdapter
Parameters:
username - the username to use
password - the password to use
Returns:
the Connection
Throws:
SQLException
See Also:
createConnectionSpec(java.lang.Integer, java.lang.Boolean, java.lang.String, java.lang.String), com.ibm.websphere.rsadapter.WSDataSource#getConnection(com.ibm.websphere.rsadapter.JDBCConnectionSpec)

createConnectionSpec

protected Object createConnectionSpec(Integer isolationLevel,
                                      Boolean readOnlyFlag,
                                      String username,
                                      String password)
                               throws SQLException
Create a WebSphere JDBCConnectionSpec object for the given charateristics.

The default implementation uses reflection to apply the given settings. Can be overridden in subclasses to customize the JDBCConnectionSpec object (JDBCConnectionSpec javadoc; IBM developerWorks article).

Parameters:
isolationLevel - the isolation level to apply (or null if none)
readOnlyFlag - the read-only flag to apply (or null if none)
username - the username to apply (null or empty indicates the default)
password - the password to apply (may be null or empty)
Throws:
SQLException - if thrown by JDBCConnectionSpec API methods
See Also:
com.ibm.websphere.rsadapter.JDBCConnectionSpec