net.ttddyy.dsproxy.support.ProxyConnectionAdvice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datasource-proxy Show documentation
Show all versions of datasource-proxy Show documentation
Provide a datasource proxy that can inject your own logic into all queries.
package net.ttddyy.dsproxy.support;
import net.ttddyy.dsproxy.proxy.JdbcProxyFactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.sql.Connection;
/**
* Support injecting proxies by AOP.
*
* @author Tadaya Tsuyukubo
*/
public class ProxyConnectionAdvice implements MethodInterceptor {
private JdbcProxyFactory jdbcProxyFactory = JdbcProxyFactory.DEFAULT;
public Object invoke(MethodInvocation invocation) throws Throwable {
Object retVal = invocation.proceed();
// only when return value is connection, return proxy.
if (!(retVal instanceof Connection)) {
return retVal;
}
return jdbcProxyFactory.createConnection((Connection) retVal, null);
}
public JdbcProxyFactory getJdbcProxyFactory() {
return jdbcProxyFactory;
}
public void setJdbcProxyFactory(JdbcProxyFactory jdbcProxyFactory) {
this.jdbcProxyFactory = jdbcProxyFactory;
}
}