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