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