1
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
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 }