| DataAccess.java |
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 con) {
62 cleanUp(con, null, null);
63 }
64
65 public static void cleanUp(Connection con, Statement s) {
66 cleanUp(con, s, null);
67 }
68
69 public static void cleanUp(Connection con, Statement s, ResultSet rs) {
70 try {
71 if (rs != null) {
72 rs.close();
73 }
74 }
75 catch (SQLException sqle) {
76 if (_log.isWarnEnabled()) {
77 _log.warn(sqle.getMessage());
78 }
79 }
80
81 try {
82 if (s != null) {
83 s.close();
84 }
85 }
86 catch (SQLException sqle) {
87 if (_log.isWarnEnabled()) {
88 _log.warn(sqle.getMessage());
89 }
90 }
91
92 try {
93 if (con != null) {
94 con.close();
95 }
96 }
97 catch (SQLException sqle) {
98 if (_log.isWarnEnabled()) {
99 _log.warn(sqle.getMessage());
100 }
101 }
102 }
103
104 private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
105
106 }