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