001    /**
002     * Copyright (c) 2000-2013 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            }
048    
049            public Object invoke(Object proxy, Method method, Object[] arguments)
050                    throws Throwable {
051    
052                    try {
053                            String methodName = method.getName();
054    
055                            if (methodName.equals("prepareStatement") &&
056                                    (arguments.length == 1)) {
057    
058                                    return prepareStatement((String)arguments[0]);
059                            }
060    
061                            return method.invoke(_connection, arguments);
062                    }
063                    catch (InvocationTargetException ite) {
064                            throw ite.getTargetException();
065                    }
066            }
067    
068            protected PreparedStatement prepareStatement(String sql)
069                    throws SQLException {
070    
071                    Thread currentThread = Thread.currentThread();
072    
073                    ClassLoader classLoader = currentThread.getContextClassLoader();
074    
075                    sql = PortalUtil.transformSQL(sql);
076    
077                    if (!_useUpgradeOptimizedPreparedStatementHandler) {
078                            return _connection.prepareStatement(sql);
079                    }
080    
081                    PreparedStatement preparedStatement = _connection.prepareStatement(
082                            sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
083    
084                    return (PreparedStatement)ProxyUtil.newProxyInstance(
085                            classLoader, new Class[] {PreparedStatement.class},
086                            new UpgradeOptimizedPreparedStatementHandler(preparedStatement));
087            }
088    
089            private Connection _connection;
090            private boolean _useUpgradeOptimizedPreparedStatementHandler;
091    
092    }