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