1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 dataSource = InfrastructureUtil.getDataSource();
41  
42          return dataSource.getConnection();
43      }
44  
45      public static Connection getConnection(String location)
46          throws NamingException, SQLException {
47  
48          InitialContext initialContext = new InitialContext();
49  
50          DataSource dataSource = (DataSource)JNDIUtil.lookup(
51              initialContext, location);
52  
53          return dataSource.getConnection();
54      }
55  
56      public static void cleanUp(Connection connection) {
57          try {
58              if (connection != null) {
59                  connection.close();
60              }
61          }
62          catch (SQLException sqle) {
63              if (_log.isWarnEnabled()) {
64                  _log.warn(sqle.getMessage());
65              }
66          }
67      }
68  
69      public static void cleanUp(Connection connection, Statement statement) {
70          cleanUp(statement);
71          cleanUp(connection);
72      }
73  
74      public static void cleanUp(
75          Connection connection, Statement statement, ResultSet resultSet) {
76  
77          cleanUp(resultSet);
78          cleanUp(statement);
79          cleanUp(connection);
80      }
81  
82      public static void cleanUp(ResultSet resultSet) {
83          try {
84              if (resultSet != null) {
85                  resultSet.close();
86              }
87          }
88          catch (SQLException sqle) {
89              if (_log.isWarnEnabled()) {
90                  _log.warn(sqle.getMessage());
91              }
92          }
93      }
94  
95      public static void cleanUp(Statement statement) {
96          try {
97              if (statement != null) {
98                  statement.close();
99              }
100         }
101         catch (SQLException sqle) {
102             if (_log.isWarnEnabled()) {
103                 _log.warn(sqle.getMessage());
104             }
105         }
106     }
107 
108     private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
109 
110 }