0

My connection pool is declared in context.xml of tomcat server as follows :

<Resource name="jdbc/codesign" auth="Container"
          type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
          url="jdbc:oracle:thin:@abc.fjd.com"
          username="tiger" password="tiger123" maxActive="30" maxIdle="10" 
          poolPreparedStatements="true" maxOpenPreparedStatements="100"
          validationQuery="SELECT SYSDATE FROM DUAL" maxWait="10000"/>

I am initializing datasource in usual way as :

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
ds = (DataSource) envContext.lookup("jdbc/codesign");

What I want is to access the username ("tiger" in this case) at runtime. I am not seeing any such method in javax.sql.DataSource class. And when I try to get schema from connection using

Connection conn = DataSourceConnectionPool.getConnection()
conn.getSchema();

I get following error :

java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.getSchema()

And I get the same error if unwrap the connection first :

conn.unwrap(OracleConnection.class).getSchema()
java.lang.AbstractMethodError: oracle.jdbc.driver.OracleConnection.getSchema()

Is ther a way to get schema name or username from datasource or Connection?

Akhil
  • 533
  • 2
  • 11
  • 26
  • Akhil, Already answered here: * http://stackoverflow.com/questions/13341286/how-to-get-database-schema-name-when-using-oracle-jdbc-connection Cheers – Michael Couck Mar 25 '15 at 11:28

1 Answers1

1

If you are using Tomcat, your DataSoruce implementation is most likely BasicDataSource https://tomcat.apache.org/tomcat-8.0-doc/api/org/apache/tomcat/dbcp/dbcp2/BasicDataSource.html

You can cast your ds to BasicDataSource, and access the user by getUsername().

But of course try first to print the db.getClass().getName() to see what is the actual implementation.

Zielu
  • 8,312
  • 4
  • 28
  • 41
  • It is of type `org.apache.tomcat.dbcp.dbcp.BasicDataSource`. How to add this as maven dependency? I know this class is in `tomcat-bbcp.jar` file which is there in tomcat lib. But how to access it as maven dependency as it has no version number suffix. – Akhil Mar 27 '15 at 05:53
  • I don't know. Just full tomcat dependency. – Zielu Mar 27 '15 at 12:49