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.util.InfrastructureUtil;
021    
022    import java.sql.Connection;
023    import java.sql.ResultSet;
024    import java.sql.SQLException;
025    import java.sql.Statement;
026    
027    import javax.naming.InitialContext;
028    import javax.naming.NamingException;
029    
030    import javax.sql.DataSource;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class DataAccess {
036    
037            public static Connection getConnection() throws SQLException {
038                    DataSource dataSource = InfrastructureUtil.getDataSource();
039    
040                    return dataSource.getConnection();
041            }
042    
043            public static Connection getConnection(String location)
044                    throws NamingException, SQLException {
045    
046                    InitialContext initialContext = new InitialContext();
047    
048                    DataSource dataSource = (DataSource)JNDIUtil.lookup(
049                            initialContext, location);
050    
051                    return dataSource.getConnection();
052            }
053    
054            public static void cleanUp(Connection connection) {
055                    try {
056                            if (connection != null) {
057                                    connection.close();
058                            }
059                    }
060                    catch (SQLException sqle) {
061                            if (_log.isWarnEnabled()) {
062                                    _log.warn(sqle.getMessage());
063                            }
064                    }
065            }
066    
067            public static void cleanUp(Connection connection, Statement statement) {
068                    cleanUp(statement);
069                    cleanUp(connection);
070            }
071    
072            public static void cleanUp(
073                    Connection connection, Statement statement, ResultSet resultSet) {
074    
075                    cleanUp(resultSet);
076                    cleanUp(statement);
077                    cleanUp(connection);
078            }
079    
080            public static void cleanUp(ResultSet resultSet) {
081                    try {
082                            if (resultSet != null) {
083                                    resultSet.close();
084                            }
085                    }
086                    catch (SQLException sqle) {
087                            if (_log.isWarnEnabled()) {
088                                    _log.warn(sqle.getMessage());
089                            }
090                    }
091            }
092    
093            public static void cleanUp(Statement statement) {
094                    try {
095                            if (statement != null) {
096                                    statement.close();
097                            }
098                    }
099                    catch (SQLException sqle) {
100                            if (_log.isWarnEnabled()) {
101                                    _log.warn(sqle.getMessage());
102                            }
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
107    
108    }