16 March 2011

Retrieving Datasource Name with iBatis on Spring Framework on WebSphere Application Server 6.2.X

I' ve used the following code sample in order to retrieve the name of datasource that is used by our bean (SqlMapClientTemplate) to access the database. I am using Rational Software Architect 7.5.X.

// Import statements 
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;
import java.lang.reflect.Field;

// ... WHATEVER ...
try {
 TransactionAwareDataSourceProxy proxyDatasource = (TransactionAwareDataSourceProxy) getSqlMapClientTemplate().getSqlMapClient().getDataSource();
 WSJdbcDataSource dataSource =  (WSJdbcDataSource) proxyDatasource.getTargetDataSource();

 Field dataSourceField = dataSource.getClass().getDeclaredField("cm");
 Object fieldValue = dataSourceField.get(dataSource);
 System.err.println("# DataSource Details:" + fieldValue);
} catch (Exception e) {
      System.err.println("Exception occured while retrieving datasource details. Details: " + e.getMessage());  

No comments:

Post a Comment

Thx for reading! Comments are appreciated...