1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.dao.jdbc;
16  
17  import com.liferay.portal.kernel.jndi.JNDIUtil;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.InfrastructureUtil;
21  
22  import java.sql.Connection;
23  import java.sql.ResultSet;
24  import java.sql.SQLException;
25  import java.sql.Statement;
26  
27  import javax.naming.InitialContext;
28  import javax.naming.NamingException;
29  
30  import javax.sql.DataSource;
31  
32  /**
33   * <a href="DataAccess.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class DataAccess {
38  
39      public static Connection getConnection() throws SQLException {
40          DataSource ds = InfrastructureUtil.getDataSource();
41  
42          return ds.getConnection();
43      }
44  
45      public static Connection getConnection(String location)
46          throws NamingException, SQLException {
47  
48          InitialContext ctx = new InitialContext();
49  
50          DataSource ds = (DataSource)JNDIUtil.lookup(ctx, location);
51  
52          return ds.getConnection();
53      }
54  
55      public static void cleanUp(Connection connection) {
56          try {
57              if (connection != null) {
58                  connection.close();
59              }
60          }
61          catch (SQLException sqle) {
62              if (_log.isWarnEnabled()) {
63                  _log.warn(sqle.getMessage());
64              }
65          }
66      }
67  
68      public static void cleanUp(Connection connection, Statement statement) {
69          cleanUp(statement);
70          cleanUp(connection);
71      }
72  
73      public static void cleanUp(
74          Connection connection, Statement statement, ResultSet resultSet) {
75  
76          cleanUp(resultSet);
77          cleanUp(statement);
78          cleanUp(connection);
79      }
80  
81      public static void cleanUp(ResultSet resultSet) {
82          try {
83              if (resultSet != null) {
84                  resultSet.close();
85              }
86          }
87          catch (SQLException sqle) {
88              if (_log.isWarnEnabled()) {
89                  _log.warn(sqle.getMessage());
90              }
91          }
92      }
93  
94      public static void cleanUp(Statement statement) {
95          try {
96              if (statement != null) {
97                  statement.close();
98              }
99          }
100         catch (SQLException sqle) {
101             if (_log.isWarnEnabled()) {
102                 _log.warn(sqle.getMessage());
103             }
104         }
105     }
106 
107     private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
108 
109 }