1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.dao.jdbc;
21  
22  import com.liferay.portal.kernel.jndi.JNDIUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.InfrastructureUtil;
26  
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Statement;
31  
32  import javax.naming.InitialContext;
33  import javax.naming.NamingException;
34  
35  import javax.sql.DataSource;
36  
37  /**
38   * <a href="DataAccess.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class DataAccess {
44  
45      public static Connection getConnection() throws SQLException {
46          DataSource ds = InfrastructureUtil.getDataSource();
47  
48          return ds.getConnection();
49      }
50  
51      public static Connection getConnection(String location)
52          throws NamingException, SQLException {
53  
54          InitialContext ctx = new InitialContext();
55  
56          DataSource ds = (DataSource)JNDIUtil.lookup(ctx, location);
57  
58          return ds.getConnection();
59      }
60  
61      public static void cleanUp(Connection connection) {
62          try {
63              if (connection != null) {
64                  connection.close();
65              }
66          }
67          catch (SQLException sqle) {
68              if (_log.isWarnEnabled()) {
69                  _log.warn(sqle.getMessage());
70              }
71          }
72      }
73  
74      public static void cleanUp(Connection connection, Statement statement) {
75          cleanUp(statement);
76          cleanUp(connection);
77      }
78  
79      public static void cleanUp(
80          Connection connection, Statement statement, ResultSet resultSet) {
81  
82          cleanUp(resultSet);
83          cleanUp(statement);
84          cleanUp(connection);
85      }
86  
87      public static void cleanUp(ResultSet resultSet) {
88          try {
89              if (resultSet != null) {
90                  resultSet.close();
91              }
92          }
93          catch (SQLException sqle) {
94              if (_log.isWarnEnabled()) {
95                  _log.warn(sqle.getMessage());
96              }
97          }
98      }
99  
100     public static void cleanUp(Statement statement) {
101         try {
102             if (statement != null) {
103                 statement.close();
104             }
105         }
106         catch (SQLException sqle) {
107             if (_log.isWarnEnabled()) {
108                 _log.warn(sqle.getMessage());
109             }
110         }
111     }
112 
113     private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
114 
115 }