001
014
015 package com.liferay.portal.kernel.dao.jdbc;
016
017 import com.liferay.portal.kernel.jndi.JNDIUtil;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.InfrastructureUtil;
021 import com.liferay.portal.kernel.util.PropsKeys;
022 import com.liferay.portal.kernel.util.PropsUtil;
023
024 import java.sql.Connection;
025 import java.sql.ResultSet;
026 import java.sql.SQLException;
027 import java.sql.Statement;
028
029 import java.util.Properties;
030
031 import javax.naming.Context;
032 import javax.naming.InitialContext;
033 import javax.naming.NamingException;
034
035 import javax.sql.DataSource;
036
037
040 public class DataAccess {
041
042 public static void cleanUp(Connection connection) {
043 try {
044 if (connection != null) {
045 connection.close();
046 }
047 }
048 catch (SQLException sqle) {
049 if (_log.isWarnEnabled()) {
050 _log.warn(sqle.getMessage());
051 }
052 }
053 }
054
055 public static void cleanUp(Connection connection, Statement statement) {
056 cleanUp(statement);
057 cleanUp(connection);
058 }
059
060 public static void cleanUp(
061 Connection connection, Statement statement, ResultSet resultSet) {
062
063 cleanUp(resultSet);
064 cleanUp(statement);
065 cleanUp(connection);
066 }
067
068 public static void cleanUp(ResultSet resultSet) {
069 try {
070 if (resultSet != null) {
071 resultSet.close();
072 }
073 }
074 catch (SQLException sqle) {
075 if (_log.isWarnEnabled()) {
076 _log.warn(sqle.getMessage());
077 }
078 }
079 }
080
081 public static void cleanUp(Statement statement) {
082 try {
083 if (statement != null) {
084 statement.close();
085 }
086 }
087 catch (SQLException sqle) {
088 if (_log.isWarnEnabled()) {
089 _log.warn(sqle.getMessage());
090 }
091 }
092 }
093
094 public static Connection getConnection() throws SQLException {
095 DataSource dataSource = InfrastructureUtil.getDataSource();
096
097 return dataSource.getConnection();
098 }
099
100 public static Connection getConnection(String location)
101 throws NamingException, SQLException {
102
103 Properties properties = PropsUtil.getProperties(
104 PropsKeys.JNDI_ENVIRONMENT, true);
105
106 Context context = new InitialContext(properties);
107
108 DataSource dataSource = (DataSource)JNDIUtil.lookup(context, location);
109
110 return dataSource.getConnection();
111 }
112
113 private static Log _log = LogFactoryUtil.getLog(DataAccess.class);
114
115 }