| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hivetranse.transaction.jdbc; |
| 16 | |
| 17 | import java.sql.Connection; |
| 18 | import java.sql.SQLException; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Iterator; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import javax.sql.DataSource; |
| 24 | |
| 25 | import org.apache.commons.logging.Log; |
| 26 | import org.apache.commons.logging.LogFactory; |
| 27 | |
| 28 | /** |
| 29 | * Actual implementation class for <code>ConnectionsRepository</code>. |
| 30 | * Enlisted connections are stored in a <code>Map</code>. |
| 31 | * |
| 32 | * @author Jean-Francois Poilpret |
| 33 | */ |
| 34 | public class ConnectionsRepositoryImpl implements ConnectionsRepository |
| 35 | { |
| 36 | static final private Log _logger = LogFactory.getLog(ConnectionsRepositoryImpl.class); |
| 37 | |
| 38 | public Connection getConnection(String id, DataSource ds) |
| 39 | throws SQLException |
| 40 | { |
| 41 | Connection cnx = _connections.get(id); |
| 42 | if (cnx == null) |
| 43 | { |
| 44 | try |
| 45 | { |
| 46 | cnx = ds.getConnection(); |
| 47 | _connections.put(id, cnx); |
| 48 | } |
| 49 | catch (SQLException e) |
| 50 | { |
| 51 | _logger.warn("getConnection", e); |
| 52 | throw e; |
| 53 | } |
| 54 | } |
| 55 | return cnx; |
| 56 | } |
| 57 | |
| 58 | public void endAllConnections(boolean close, boolean commit) |
| 59 | throws SQLException |
| 60 | { |
| 61 | SQLException exc = null; |
| 62 | SQLException e; |
| 63 | Iterator<Map.Entry<String, Connection>> i = _connections.entrySet().iterator(); |
| 64 | while (i.hasNext()) |
| 65 | { |
| 66 | Map.Entry<String, Connection> entry = i.next(); |
| 67 | String id = entry.getKey(); |
| 68 | Connection cnx = entry.getValue(); |
| 69 | e = terminateConnection(id, cnx, commit); |
| 70 | if (e != null && exc == null) |
| 71 | { |
| 72 | // We keep track only of the first exception occurring |
| 73 | exc = e; |
| 74 | } |
| 75 | if (close) |
| 76 | { |
| 77 | e = closeConnection(id, cnx); |
| 78 | if (e != null && exc == null) |
| 79 | { |
| 80 | // We keep track only of the first exception occurring |
| 81 | exc = e; |
| 82 | } |
| 83 | i.remove(); |
| 84 | } |
| 85 | } |
| 86 | if (exc != null) |
| 87 | { |
| 88 | throw exc; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | protected SQLException terminateConnection(String id, Connection cnx, boolean commit) |
| 93 | { |
| 94 | try |
| 95 | { |
| 96 | if (commit) |
| 97 | { |
| 98 | cnx.commit(); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | cnx.rollback(); |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | catch (SQLException e) |
| 107 | { |
| 108 | _logger.warn("terminateConnection", e); |
| 109 | return e; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | protected SQLException closeConnection(String id, Connection cnx) |
| 114 | { |
| 115 | try |
| 116 | { |
| 117 | cnx.close(); |
| 118 | return null; |
| 119 | } |
| 120 | catch (SQLException e) |
| 121 | { |
| 122 | _logger.warn("closeConnection", e); |
| 123 | return e; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private final Map<String, Connection> _connections = new HashMap<String, Connection>(); |
| 128 | } |