connection checker java class in spring boot using HikariCP - spring-boot

Jboss have a mechanism where by I can execute a connection checker class(OracleValidConnectionChecker) before a connection is checked out from the connection pool. That would be helpful in calling a stored procedure before each DB call.(This is needed for setting security context in DB layer for Oracle Layer Security)
Is there any similar mechanism in spring boot (tomcat) using HikariCP ?
I know that there is a connection checker SQL query config (spring.datasource.hikari.connection-test-query). But I am looking for a way to execute a Procedure with input parameters.

As per https://github.com/brettwooldridge/HikariCP#configuration-knobs-baby I believe connectionInitSql would make more sense in your case. connectionTestQuery is for "legacy" drivers that do not support the JDBC4 Connection.isValid().
You may be able to construct your own stored procedure query and pass it into this property, however, be wary you will not benefit from the protection prepared statements generally provide eg. sql injection.

Related

Is it possible to disable a Spring Boot datasource configuration based on unavailability of a connection to a DB

We have an application that uses several data sources. A DB underlying one of those data sources is down at the moment: IOError. Network adapter couldn't establish the connection & Socket read timed out.
Is there an annotation (or other means) of configuring Spring Boot such that it bypasses the culprit data source and still starts up: the DB is not essential in current development work. spring.datasource.continue-on-error=true doesn't seem to work. This is Spring 2.2.2.RELEASE.
using multiple datasource, so when your apps fail at start up your apps still work, i mean using memory db / sqlite to handle fail at connection error...

How to use Spring transactions with plain old jdbc connection without Spring jdbc template?

We have an old EJB based web application which we are supposed to migrate into Spring boot. For database connectivity, it is a plain old jdbc connection approach as of now.
We want to use Spring transactions and remove EJB transactions but willing to keep plain old jdbc connectivity same. In short we don't want to make changes in our DAO layer to convert plain old jdbc to Spring JdbcTemplate.
Please note that we have our own connection pooling algorithm and we create connection object and close it in the pool.
Along with this we want our application to be multi-tenant that can work with multiple databases on the basis of 'tenantID' that we will provide.
I actually tried to implement this but it is not working. I had to manually do con.commit(); and con.rollback();
Is there any way to use Spring transactions with plain old jdbc connectivity with above scenario?

Does Spring JDBC supports XA connection pools (for example, Oracle UCP)?

I'd like to add XA connection pool to my Spring based application, specifically Oracle UCP. This pool works on XADataSource and XAConnection objects. Standard DataSource.getConnection(...) methods are not supported.
Trying working with that pool (that I am able to successfully set up in Spring) I am getting an error related to the fact that getConnection(...) methods invoked by JdbcTemplate are disabled and should not be used. I am just wondering whether any of Spring JDBC classes are able to work with XADataSource and XAConnections? Probably there are another ways to use Spring with XA connection pools? Will appreciate any advices on that topic.
Just for those who may experience similar problems - Spring doesn't support directly XA connection pools (through native XADataSource.getXAConnection(...) methods). You need to write a kind of wrapper utilizing DatSource.getConnection(...) methods.

MyBatis+Spring+Jersey with Oracle Seems to reuses SQL sessions

I'm creating a rest service using MyBatis 3.3.1, Spring 4.3, Jersey 2.22 and Oracle 12c. My transactions are being managed by Spring using the DataSourceTransactionManager and the #Transaction annotaion. I am also using a MyBatis pooled data source as my javax.sql.DataSource. What I don't understand if why database sessions are being reused.
In my application, I am setting an oracle client identifier: DBMS_SESSION.SET_IDENTIFIER("my id"). With the debug logging statements, I can see MyBatis creating new sessions for each of the MyBatis sql operations. I also have debug to print out the database session identifier from DBMS_SESSION.UNIQUE_SESSION_ID.
What I don't understand is that if I access my rest endpoint multiple times, the unique session id is the same and the identifier from my last access is still set.
Shouldn't a new oracle session be used every time MyBatis gets a new SQLSession? Why is the oracle session always the same?
Thanks.
Session in Oracle is bound to a connection.
You are using connection pooling so after one rest request is finished the connection is returned to the connection pool. Session is not terminated in this case.
You probably want to clear identifier on returning connection to pool and setting it on retrieving connection from the pool. The exact way to do that depends on the connection pool you are using. For built-in mybatis connection pool see this answer.

Force encrypted network traffic with Oracle thin client and jdbc?

Is there a way to force encryption of network traffic (that is, result set data) using Oracle thin client and jdbc?
I understand that this can be done by setting up a java.util.Properties object and passing that to DriverManager.getConnection( String, Properties), but is there a way to specify this in the jdbc url?
I'm using a third party tool written in Java, which handles creating its own connections, so creating and passing the Properties object won't work for me.
Thanks.
Have a look at the Oracle JDBC documentation. There is a chapter about Client Side Security Features, that talks about using system properties to configure a Thin Driver for SSL.

Resources