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
46 public class DataAccess {
47
48 public static Connection getConnection() throws SQLException {
49 DataSource ds = InfrastructureUtil.getDataSource();
50
51 return ds.getConnection();
52 }
53
54 public static Connection getConnection(String location)
55 throws NamingException, SQLException {
56
57 InitialContext ctx = new InitialContext();
58
59 DataSource ds = (DataSource)JNDIUtil.lookup(ctx, location);
60
61 return ds.getConnection();
62 }
63
64 public static void cleanUp(Connection connection) {
65 try {
66 if (connection != null) {
67 connection.close();
68 }
69 }
70 catch (SQLException sqle) {
71 if (_log.isWarnEnabled()) {
72 _log.warn(sqle.getMessage());
73 }
74 }
75 }
76
77 public static void cleanUp(Connection connection, Statement statement) {
78 cleanUp(statement);
79 cleanUp(connection);
80 }
81
82 public static void cleanUp(
83 Connection connection, Statement statement, ResultSet resultSet) {
84
85 cleanUp(resultSet);
86 cleanUp(statement);
87 cleanUp(connection);
88 }
89
90 public static void cleanUp(ResultSet resultSet) {
91 try {
92 if (resultSet != null) {
93 resultSet.close();
94 }
95 }
96 catch (SQLException sqle) {
97 if (_log.isWarnEnabled()) {
98 _log.warn(sqle.getMessage());
99 }
100 }
101 }
102
103 public static void cleanUp(Statement statement) {
104 try {
105 if (statement != null) {
106 statement.close();
107 }
108 }
109 catch (SQLException sqle) {
110 if (_log.isWarnEnabled()) {
111 _log.warn(sqle.getMessage());
112 }
113 }
114 }
115
116 private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
117
118 }