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.dao.jdbc;
016    
017    import com.liferay.portal.kernel.jndi.JNDIUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.upgrade.dao.orm.UpgradeOptimizedConnectionHandler;
021    import com.liferay.portal.kernel.util.InfrastructureUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.PropsUtil;
024    import com.liferay.portal.kernel.util.ProxyUtil;
025    
026    import java.sql.Connection;
027    import java.sql.DatabaseMetaData;
028    import java.sql.ResultSet;
029    import java.sql.SQLException;
030    import java.sql.Statement;
031    
032    import java.util.Properties;
033    
034    import javax.naming.Context;
035    import javax.naming.InitialContext;
036    import javax.naming.NamingException;
037    
038    import javax.sql.DataSource;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     */
043    public class DataAccess {
044    
045            public static void cleanUp(Connection connection) {
046                    try {
047                            if (connection != null) {
048                                    connection.close();
049                            }
050                    }
051                    catch (SQLException sqle) {
052                            if (_log.isWarnEnabled()) {
053                                    _log.warn(sqle.getMessage());
054                            }
055                    }
056            }
057    
058            public static void cleanUp(Connection connection, Statement statement) {
059                    cleanUp(statement);
060                    cleanUp(connection);
061            }
062    
063            public static void cleanUp(
064                    Connection connection, Statement statement, ResultSet resultSet) {
065    
066                    cleanUp(resultSet);
067                    cleanUp(statement);
068                    cleanUp(connection);
069            }
070    
071            public static void cleanUp(ResultSet resultSet) {
072                    try {
073                            if (resultSet != null) {
074                                    resultSet.close();
075                            }
076                    }
077                    catch (SQLException sqle) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn(sqle.getMessage());
080                            }
081                    }
082            }
083    
084            public static void cleanUp(Statement statement) {
085                    try {
086                            if (statement != null) {
087                                    statement.close();
088                            }
089                    }
090                    catch (SQLException sqle) {
091                            if (_log.isWarnEnabled()) {
092                                    _log.warn(sqle.getMessage());
093                            }
094                    }
095            }
096    
097            public static Connection getConnection() throws SQLException {
098                    DataSource dataSource = InfrastructureUtil.getDataSource();
099    
100                    return dataSource.getConnection();
101            }
102    
103            public static Connection getConnection(String location)
104                    throws NamingException, SQLException {
105    
106                    Properties properties = PropsUtil.getProperties(
107                            PropsKeys.JNDI_ENVIRONMENT, true);
108    
109                    Context context = new InitialContext(properties);
110    
111                    DataSource dataSource = (DataSource)JNDIUtil.lookup(context, location);
112    
113                    return dataSource.getConnection();
114            }
115    
116            public static Connection getUpgradeOptimizedConnection()
117                    throws SQLException {
118    
119                    Connection con = getConnection();
120    
121                    DatabaseMetaData metaData = con.getMetaData();
122    
123                    String productName = metaData.getDatabaseProductName();
124    
125                    if (!productName.equals("Microsoft SQL Server")) {
126                            return con;
127                    }
128    
129                    Thread currentThread = Thread.currentThread();
130    
131                    ClassLoader classLoader = currentThread.getContextClassLoader();
132    
133                    return (Connection)ProxyUtil.newProxyInstance(
134                            classLoader, new Class[] {Connection.class},
135                            new UpgradeOptimizedConnectionHandler(con));
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
139    
140    }