001
014
015 package com.liferay.portal.kernel.upgrade.dao.orm;
016
017 import com.liferay.portal.kernel.util.ProxyUtil;
018
019 import java.lang.reflect.InvocationHandler;
020 import java.lang.reflect.InvocationTargetException;
021 import java.lang.reflect.Method;
022
023 import java.sql.Connection;
024 import java.sql.PreparedStatement;
025 import java.sql.ResultSet;
026 import java.sql.SQLException;
027
028
031 public class UpgradeOptimizedConnectionHandler implements InvocationHandler {
032
033 public UpgradeOptimizedConnectionHandler(Connection connection) {
034 _connection = connection;
035 }
036
037 public Object invoke(Object proxy, Method method, Object[] arguments)
038 throws Throwable {
039
040 try {
041 String methodName = method.getName();
042
043 if (methodName.equals("prepareStatement") &&
044 (arguments.length == 1)) {
045
046 return prepareStatement((String)arguments[0]);
047 }
048
049 return method.invoke(_connection, arguments);
050 }
051 catch (InvocationTargetException ite) {
052 throw ite.getTargetException();
053 }
054 }
055
056 protected PreparedStatement prepareStatement(String sql)
057 throws SQLException {
058
059 Thread currentThread = Thread.currentThread();
060
061 ClassLoader classLoader = currentThread.getContextClassLoader();
062
063 PreparedStatement preparedStatement = _connection.prepareStatement(
064 sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
065
066 return (PreparedStatement)ProxyUtil.newProxyInstance(
067 classLoader, new Class[] {PreparedStatement.class},
068 new UpgradeOptimizedPreparedStatementHandler(preparedStatement));
069 }
070
071 private Connection _connection;
072
073 }