Reputation: 4130
I am trying to create a very simple service via Spring Boot that involves writing a value to an Oracle database. This is supposedly very simple with a Hikari connection pool. The service will use a JdbcTemplate to call a stored procedure. Again, simple.
What's NOT simple is the fact that as part of the request to the database, I need to set the client info (OracleConnection.setClientInfo()). I'm wanting to configure the connection pool ONCE so that any connection retried will have this value set. I'm trying to avoid manually unwrapping the connection or using some AOP-based solution.
Is there a simple way to do what I need?
Upvotes: 1
Views: 1073
Reputation: 31
I've been investigating the same, and here is my current findings:
import javax.sql.DataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ApplicationBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if(bean instanceof DataSource) {
return new MyDataSourceWrapper((DataSource)bean);
}
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DelegatingDataSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
public class MyDataSourceWrapper extends DelegatingDataSource {
public MyDataSourceWrapper(DataSource dataSource) {
super(dataSource);
}
@Override
public Connection getConnection() throws SQLException {
Connection connection = super.getConnection();
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
connection.setClientInfo("OCSID.CLIENTID", currentPrincipalName);
return connection;
}
}
On the database-side I can access this with
sys_context('USERENV', 'CLIENT_IDENTIFIER')
Upvotes: 3