001    /**
002     * Copyright (c) 2000-present 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    import com.liferay.portal.util.PortalUtil;
019    
020    import java.lang.reflect.InvocationHandler;
021    import java.lang.reflect.InvocationTargetException;
022    import java.lang.reflect.Method;
023    
024    import java.sql.Connection;
025    import java.sql.DatabaseMetaData;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    import java.sql.SQLException;
029    
030    /**
031     * @author Minhchau Dang
032     */
033    public class UpgradeOptimizedConnectionHandler implements InvocationHandler {
034    
035            public UpgradeOptimizedConnectionHandler(Connection connection)
036                    throws SQLException {
037    
038                    _connection = connection;
039    
040                    DatabaseMetaData metaData = _connection.getMetaData();
041    
042                    String productName = metaData.getDatabaseProductName();
043    
044                    if (productName.equals("Microsoft SQL Server")) {
045                            _useUpgradeOptimizedPreparedStatementHandler = true;
046                    }
047                    else {
048                            _useUpgradeOptimizedPreparedStatementHandler = false;
049                    }
050            }
051    
052            @Override
053            public Object invoke(Object proxy, Method method, Object[] arguments)
054                    throws Throwable {
055    
056                    try {
057                            String methodName = method.getName();
058    
059                            if (methodName.equals("prepareStatement") &&
060                                    (arguments.length == 1)) {
061    
062                                    return prepareStatement((String)arguments[0]);
063                            }
064    
065                            return method.invoke(_connection, arguments);
066                    }
067                    catch (InvocationTargetException ite) {
068                            throw ite.getTargetException();
069                    }
070            }
071    
072            protected PreparedStatement prepareStatement(String sql)
073                    throws SQLException {
074    
075                    Thread currentThread = Thread.currentThread();
076    
077                    ClassLoader classLoader = currentThread.getContextClassLoader();
078    
079                    sql = PortalUtil.transformSQL(sql);
080    
081                    if (!_useUpgradeOptimizedPreparedStatementHandler) {
082                            return _connection.prepareStatement(sql);
083                    }
084    
085                    PreparedStatement preparedStatement = _connection.prepareStatement(
086                            sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
087    
088                    return (PreparedStatement)ProxyUtil.newProxyInstance(
089                            classLoader, new Class[] {PreparedStatement.class},
090                            new UpgradeOptimizedPreparedStatementHandler(preparedStatement));
091            }
092    
093            private final Connection _connection;
094            private final boolean _useUpgradeOptimizedPreparedStatementHandler;
095    
096    }