How to get database configurations using data source object in java

I am stuck in a problem related to data source objects in java. I set the data source connection parameters in the data source object (org.apache.tomcat.jdbc.pool.DataSource). I want to get these parameters from a data source object before I call the getConnection method method containing meaningful debugging information inside catch if it catches an exception.

Below is the code I tried. I can get all connection parameters from metadata as follows: [ex: - connection.getMetaData (). GetURL ()], but I want to catch the exception and log url, password, username as a log if getConnection () throws an exception. Therefore, I need to get this information from the data source object before it tries to create a db connection.

try { // try to get the lookup name. If error empty string will be returned jndiLookupName = connectionProperties.getProperty(RDBMSConstants.PROP_JNDI_LOOKUP_NAME); datasource = InitialContext.doLookup(jndiLookupName); connection = datasource.getConnection(); // WHEN THIS THROWS EXCEPTION... logger.info(connection.getMetaData().getURL()); // these won't work since exception already thrown. logger.info(connection.getMetaData().getUserName()); logger.info(connection.getMetaData().getDriverName()); logger.info(connection.getMetaData().getDriverVersion()); isConnected = true; // if no errors logger.info("JDBC connection established with jndi config " + jndiLookupName); } catch (SQLException e) { //...I WANT ALL CONNECTION PARAMETERS (URL,PASSWORD,USERNAME) HERE throw new SQLException("Connecting to database failed with jndi lookup", e); } 

When I remove debugging, I get the data source object as follows.

 org.apache.tomcat.jdbc.pool.DataSource@d47feb3 {ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=com.mysql.jdbc.Driver; maxActive=100; maxIdle=8; minIdle=0; initialSize=0; maxWait=30000; testOnBorrow=false; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:mysql://localhost/wso2_mb_1; username=a; validationQuery=null; validationQueryTimeout=-1; validatorClassName=null; validationInterval=30000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=ConnectionState;StatementFinalizer;org.wso2.carbon.ndatasource.rdbms.ConnectionRollbackOnReturnInterceptor;; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; } 

I see all url, username and password parameters, but I can not get them. Is there a way to get these values โ€‹โ€‹from a data source object.

+5
source share
2 answers

Transferring a specific implementation of DataSource - a data source combined with tomcat provides access to username , url , etc. (see DataSource for more information):

 if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) { org.apache.tomcat.jdbc.pool.DataSource tcDataSource = (org.apache.tomcat.jdbc.pool.DataSource)dataSource; logger.info(tcDataSource.getUrl()); logger.info(tcDataSource.getUsername()); ... } 
+5
source

I donโ€™t think you could do this, since the DataSource is just an abstraction and is implemented by the driver provider, but just by connecting a factory. You should try to get the parameters elsewhere, for example, a configuration properties file or such

-1
source

Source: https://habr.com/ru/post/1213925/


All Articles