001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 ds = InfrastructureUtil.getDataSource();
039    
040                    return ds.getConnection();
041            }
042    
043            public static Connection getConnection(String location)
044                    throws NamingException, SQLException {
045    
046                    InitialContext ctx = new InitialContext();
047    
048                    DataSource ds = (DataSource)JNDIUtil.lookup(ctx, location);
049    
050                    return ds.getConnection();
051            }
052    
053            public static void cleanUp(Connection connection) {
054                    try {
055                            if (connection != null) {
056                                    connection.close();
057                            }
058                    }
059                    catch (SQLException sqle) {
060                            if (_log.isWarnEnabled()) {
061                                    _log.warn(sqle.getMessage());
062                            }
063                    }
064            }
065    
066            public static void cleanUp(Connection connection, Statement statement) {
067                    cleanUp(statement);
068                    cleanUp(connection);
069            }
070    
071            public static void cleanUp(
072                    Connection connection, Statement statement, ResultSet resultSet) {
073    
074                    cleanUp(resultSet);
075                    cleanUp(statement);
076                    cleanUp(connection);
077            }
078    
079            public static void cleanUp(ResultSet resultSet) {
080                    try {
081                            if (resultSet != null) {
082                                    resultSet.close();
083                            }
084                    }
085                    catch (SQLException sqle) {
086                            if (_log.isWarnEnabled()) {
087                                    _log.warn(sqle.getMessage());
088                            }
089                    }
090            }
091    
092            public static void cleanUp(Statement statement) {
093                    try {
094                            if (statement != null) {
095                                    statement.close();
096                            }
097                    }
098                    catch (SQLException sqle) {
099                            if (_log.isWarnEnabled()) {
100                                    _log.warn(sqle.getMessage());
101                            }
102                    }
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
106    
107    }