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