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