001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
029     * @author Minhchau Dang
030     */
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    }