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