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.upgrade.dao.orm.UpgradeOptimizedConnectionHandler;
021 import com.liferay.portal.kernel.util.InfrastructureUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.PropsUtil;
024 import com.liferay.portal.kernel.util.ProxyUtil;
025
026 import java.sql.Connection;
027 import java.sql.ResultSet;
028 import java.sql.SQLException;
029 import java.sql.Statement;
030
031 import java.util.Properties;
032
033 import javax.naming.Context;
034 import javax.naming.InitialContext;
035 import javax.naming.NamingException;
036
037 import javax.sql.DataSource;
038
039
042 public class DataAccess {
043
044 public static void cleanUp(Connection connection) {
045 try {
046 if (connection != null) {
047 connection.close();
048 }
049 }
050 catch (SQLException sqle) {
051 if (_log.isWarnEnabled()) {
052 _log.warn(sqle.getMessage());
053 }
054 }
055 }
056
057 public static void cleanUp(Connection connection, Statement statement) {
058 cleanUp(statement);
059
060 cleanUp(connection);
061 }
062
063 public static void cleanUp(
064 Connection connection, Statement statement, ResultSet resultSet) {
065
066 cleanUp(resultSet);
067
068 cleanUp(statement);
069
070 cleanUp(connection);
071 }
072
073 public static void cleanUp(ResultSet resultSet) {
074 try {
075 if (resultSet != null) {
076 resultSet.close();
077 }
078 }
079 catch (SQLException sqle) {
080 if (_log.isWarnEnabled()) {
081 _log.warn(sqle.getMessage());
082 }
083 }
084 }
085
086 public static void cleanUp(Statement statement) {
087 try {
088 if (statement != null) {
089 statement.close();
090 }
091 }
092 catch (SQLException sqle) {
093 if (_log.isWarnEnabled()) {
094 _log.warn(sqle.getMessage());
095 }
096 }
097 }
098
099 public static void cleanUp(Statement statement, ResultSet resultSet) {
100 cleanUp(resultSet);
101
102 cleanUp(statement);
103 }
104
105 public static void deepCleanUp(ResultSet resultSet) {
106 try {
107 if (resultSet != null) {
108 Statement statement = resultSet.getStatement();
109
110 Connection connection = statement.getConnection();
111
112 cleanUp(connection, statement, resultSet);
113 }
114 }
115 catch (SQLException sqle) {
116 if (_log.isWarnEnabled()) {
117 _log.warn(sqle.getMessage());
118 }
119 }
120 }
121
122 public static Connection getConnection() throws SQLException {
123 DataSource dataSource = _pacl.getDataSource();
124
125 return dataSource.getConnection();
126 }
127
128 public static Connection getConnection(String location)
129 throws NamingException, SQLException {
130
131 DataSource dataSource = _pacl.getDataSource(location);
132
133 return dataSource.getConnection();
134 }
135
136 public static Connection getUpgradeOptimizedConnection()
137 throws SQLException {
138
139 Connection connection = getConnection();
140
141 Thread currentThread = Thread.currentThread();
142
143 ClassLoader classLoader = currentThread.getContextClassLoader();
144
145 return (Connection)ProxyUtil.newProxyInstance(
146 classLoader, new Class[] {Connection.class},
147 new UpgradeOptimizedConnectionHandler(connection));
148 }
149
150 public interface PACL {
151
152 public DataSource getDataSource();
153
154 public DataSource getDataSource(String location) throws NamingException;
155
156 }
157
158 private static final Log _log = LogFactoryUtil.getLog(DataAccess.class);
159
160 private static final PACL _pacl = new NoPACL();
161
162 private static class NoPACL implements PACL {
163
164 @Override
165 public DataSource getDataSource() {
166 return InfrastructureUtil.getDataSource();
167 }
168
169 @Override
170 public DataSource getDataSource(String location)
171 throws NamingException {
172
173 Properties properties = PropsUtil.getProperties(
174 PropsKeys.JNDI_ENVIRONMENT, true);
175
176 Context context = new InitialContext(properties);
177
178 return (DataSource)JNDIUtil.lookup(context, location);
179 }
180
181 }
182
183 }