Commit 7b8c96ca78089d8a71e714f87a1f31843a08ed88
1 parent
997354d0
Exists in
master
Ajustes para que o status da conexão seja inserido no
ConnectionProducer.
Showing
3 changed files
with
59 additions
and
16 deletions
Show diff stats
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/producer/ConnectionProducer.java
| @@ -37,6 +37,7 @@ public class ConnectionProducer implements Serializable { | @@ -37,6 +37,7 @@ public class ConnectionProducer implements Serializable { | ||
| 37 | private ResourceBundle bundle; | 37 | private ResourceBundle bundle; |
| 38 | 38 | ||
| 39 | private final Map<String, Connection> cache = Collections.synchronizedMap(new HashMap<String, Connection>()); | 39 | private final Map<String, Connection> cache = Collections.synchronizedMap(new HashMap<String, Connection>()); |
| 40 | + private final Map<Connection,Status> statusCache = Collections.synchronizedMap(new HashMap<Connection, Status>()); | ||
| 40 | 41 | ||
| 41 | @Inject | 42 | @Inject |
| 42 | private DataSourceProducer producer; | 43 | private DataSourceProducer producer; |
| @@ -79,6 +80,7 @@ public class ConnectionProducer implements Serializable { | @@ -79,6 +80,7 @@ public class ConnectionProducer implements Serializable { | ||
| 79 | disableAutoCommit(connection); | 80 | disableAutoCommit(connection); |
| 80 | 81 | ||
| 81 | cache.put(name, connection); | 82 | cache.put(name, connection); |
| 83 | + statusCache.put(connection, new Status()); | ||
| 82 | logger.info(bundle.getString("connection-was-created", name)); | 84 | logger.info(bundle.getString("connection-was-created", name)); |
| 83 | 85 | ||
| 84 | } catch (Exception cause) { | 86 | } catch (Exception cause) { |
| @@ -150,5 +152,34 @@ public class ConnectionProducer implements Serializable { | @@ -150,5 +152,34 @@ public class ConnectionProducer implements Serializable { | ||
| 150 | public Map<String, Connection> getCache() { | 152 | public Map<String, Connection> getCache() { |
| 151 | return cache; | 153 | return cache; |
| 152 | } | 154 | } |
| 155 | + | ||
| 156 | + public Status getStatus(Connection connection) { | ||
| 157 | + return statusCache.get(connection); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + public static class Status implements Serializable { | ||
| 161 | + | ||
| 162 | + private static final long serialVersionUID = 1L; | ||
| 163 | + | ||
| 164 | + private boolean active = false; | ||
| 165 | + | ||
| 166 | + private boolean markedRollback = false; | ||
| 167 | + | ||
| 168 | + public boolean isActive() { | ||
| 169 | + return active; | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + public void setActive(boolean active) { | ||
| 173 | + this.active = active; | ||
| 174 | + } | ||
| 175 | + | ||
| 176 | + public boolean isMarkedRollback() { | ||
| 177 | + return markedRollback; | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + public void setRollbackOnly(boolean markedRollback) { | ||
| 181 | + this.markedRollback = markedRollback; | ||
| 182 | + } | ||
| 183 | + } | ||
| 153 | 184 | ||
| 154 | } | 185 | } |
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/internal/proxy/ConnectionProxy.java
| @@ -27,7 +27,7 @@ public class ConnectionProxy implements Connection, Serializable { | @@ -27,7 +27,7 @@ public class ConnectionProxy implements Connection, Serializable { | ||
| 27 | private static final long serialVersionUID = 1L; | 27 | private static final long serialVersionUID = 1L; |
| 28 | 28 | ||
| 29 | private final String dataSourceName; | 29 | private final String dataSourceName; |
| 30 | - | 30 | + |
| 31 | public ConnectionProxy(String dataSourceName) { | 31 | public ConnectionProxy(String dataSourceName) { |
| 32 | this.dataSourceName = dataSourceName; | 32 | this.dataSourceName = dataSourceName; |
| 33 | } | 33 | } |
| @@ -236,4 +236,5 @@ public class ConnectionProxy implements Connection, Serializable { | @@ -236,4 +236,5 @@ public class ConnectionProxy implements Connection, Serializable { | ||
| 236 | public <T> T unwrap(Class<T> iface) throws SQLException { | 236 | public <T> T unwrap(Class<T> iface) throws SQLException { |
| 237 | return getDelegate().unwrap(iface); | 237 | return getDelegate().unwrap(iface); |
| 238 | } | 238 | } |
| 239 | + | ||
| 239 | } | 240 | } |
impl/extension/jdbc/src/main/java/br/gov/frameworkdemoiselle/transaction/JDBCTransaction.java
| @@ -38,18 +38,16 @@ package br.gov.frameworkdemoiselle.transaction; | @@ -38,18 +38,16 @@ package br.gov.frameworkdemoiselle.transaction; | ||
| 38 | 38 | ||
| 39 | import static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY; | 39 | import static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY; |
| 40 | 40 | ||
| 41 | -import java.io.Serializable; | ||
| 42 | import java.sql.Connection; | 41 | import java.sql.Connection; |
| 43 | import java.util.Collection; | 42 | import java.util.Collection; |
| 44 | -import java.util.Collections; | ||
| 45 | -import java.util.HashMap; | ||
| 46 | -import java.util.Map; | ||
| 47 | 43 | ||
| 48 | import br.gov.frameworkdemoiselle.DemoiselleException; | 44 | import br.gov.frameworkdemoiselle.DemoiselleException; |
| 49 | import br.gov.frameworkdemoiselle.annotation.Priority; | 45 | import br.gov.frameworkdemoiselle.annotation.Priority; |
| 50 | import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer; | 46 | import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer; |
| 47 | +import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer.Status; | ||
| 51 | import br.gov.frameworkdemoiselle.util.Beans; | 48 | import br.gov.frameworkdemoiselle.util.Beans; |
| 52 | 49 | ||
| 50 | + | ||
| 53 | /** | 51 | /** |
| 54 | * Represents the strategy destinated to manage JDBC transactions. | 52 | * Represents the strategy destinated to manage JDBC transactions. |
| 55 | * | 53 | * |
| @@ -63,17 +61,22 @@ public class JDBCTransaction implements Transaction { | @@ -63,17 +61,22 @@ public class JDBCTransaction implements Transaction { | ||
| 63 | 61 | ||
| 64 | private ConnectionProducer producer; | 62 | private ConnectionProducer producer; |
| 65 | 63 | ||
| 66 | - private Map<Connection, Status> cache = Collections.synchronizedMap(new HashMap<Connection, Status>()); | 64 | + //private Map<Connection, Status> cache = Collections.synchronizedMap(new HashMap<Connection, Status>()); |
| 65 | + //private List<ConnectionProxy> cache = Collections.synchronizedList(new ArrayList<ConnectionProxy>()); | ||
| 67 | 66 | ||
| 68 | private ConnectionProducer getProducer() { | 67 | private ConnectionProducer getProducer() { |
| 69 | if (producer == null) { | 68 | if (producer == null) { |
| 70 | producer = Beans.getReference(ConnectionProducer.class); | 69 | producer = Beans.getReference(ConnectionProducer.class); |
| 71 | 70 | ||
| 72 | - for (Connection connection : producer.getCache().values()) { | ||
| 73 | - if (!cache.containsKey(connection)) { | ||
| 74 | - cache.put(connection, new Status()); | 71 | + /*for (Connection connection : producer.getCache().values()) { |
| 72 | + if (!ConnectionProxy.class.isInstance(connection)) { | ||
| 73 | + continue; | ||
| 75 | } | 74 | } |
| 76 | - } | 75 | + |
| 76 | + if (!cache.contains(connection)) { | ||
| 77 | + cache.add((ConnectionProxy)connection); | ||
| 78 | + } | ||
| 79 | + }*/ | ||
| 77 | } | 80 | } |
| 78 | 81 | ||
| 79 | return producer; | 82 | return producer; |
| @@ -87,7 +90,7 @@ public class JDBCTransaction implements Transaction { | @@ -87,7 +90,7 @@ public class JDBCTransaction implements Transaction { | ||
| 87 | public void begin() { | 90 | public void begin() { |
| 88 | Status status; | 91 | Status status; |
| 89 | for (Connection connection : getDelegate()) { | 92 | for (Connection connection : getDelegate()) { |
| 90 | - status = cache.get(connection); | 93 | + status = getProducer().getStatus(connection); |
| 91 | status.setActive(true); | 94 | status.setActive(true); |
| 92 | } | 95 | } |
| 93 | } | 96 | } |
| @@ -97,9 +100,13 @@ public class JDBCTransaction implements Transaction { | @@ -97,9 +100,13 @@ public class JDBCTransaction implements Transaction { | ||
| 97 | */ | 100 | */ |
| 98 | @Override | 101 | @Override |
| 99 | public void commit() { | 102 | public void commit() { |
| 103 | + Status status; | ||
| 104 | + | ||
| 100 | for (Connection connection : getDelegate()) { | 105 | for (Connection connection : getDelegate()) { |
| 101 | try { | 106 | try { |
| 102 | connection.commit(); | 107 | connection.commit(); |
| 108 | + status = getProducer().getStatus(connection); | ||
| 109 | + status.setActive(false); | ||
| 103 | } catch (Exception cause) { | 110 | } catch (Exception cause) { |
| 104 | throw new DemoiselleException(cause); | 111 | throw new DemoiselleException(cause); |
| 105 | } | 112 | } |
| @@ -111,9 +118,13 @@ public class JDBCTransaction implements Transaction { | @@ -111,9 +118,13 @@ public class JDBCTransaction implements Transaction { | ||
| 111 | */ | 118 | */ |
| 112 | @Override | 119 | @Override |
| 113 | public void rollback() { | 120 | public void rollback() { |
| 121 | + Status status; | ||
| 122 | + | ||
| 114 | for (Connection connection : getDelegate()) { | 123 | for (Connection connection : getDelegate()) { |
| 115 | try { | 124 | try { |
| 116 | connection.rollback(); | 125 | connection.rollback(); |
| 126 | + status = getProducer().getStatus(connection); | ||
| 127 | + status.setActive(false); | ||
| 117 | } catch (Exception cause) { | 128 | } catch (Exception cause) { |
| 118 | throw new DemoiselleException(cause); | 129 | throw new DemoiselleException(cause); |
| 119 | } | 130 | } |
| @@ -124,7 +135,7 @@ public class JDBCTransaction implements Transaction { | @@ -124,7 +135,7 @@ public class JDBCTransaction implements Transaction { | ||
| 124 | public void setRollbackOnly() { | 135 | public void setRollbackOnly() { |
| 125 | Status status; | 136 | Status status; |
| 126 | for (Connection connection : getDelegate()) { | 137 | for (Connection connection : getDelegate()) { |
| 127 | - status = cache.get(connection); | 138 | + status = getProducer().getStatus(connection); |
| 128 | status.setRollbackOnly(true); | 139 | status.setRollbackOnly(true); |
| 129 | } | 140 | } |
| 130 | } | 141 | } |
| @@ -135,7 +146,7 @@ public class JDBCTransaction implements Transaction { | @@ -135,7 +146,7 @@ public class JDBCTransaction implements Transaction { | ||
| 135 | boolean result = true; | 146 | boolean result = true; |
| 136 | 147 | ||
| 137 | for (Connection connection : getDelegate()) { | 148 | for (Connection connection : getDelegate()) { |
| 138 | - status = cache.get(connection); | 149 | + status = getProducer().getStatus(connection); |
| 139 | result = result && status.isActive(); | 150 | result = result && status.isActive(); |
| 140 | } | 151 | } |
| 141 | 152 | ||
| @@ -148,14 +159,14 @@ public class JDBCTransaction implements Transaction { | @@ -148,14 +159,14 @@ public class JDBCTransaction implements Transaction { | ||
| 148 | boolean result = true; | 159 | boolean result = true; |
| 149 | 160 | ||
| 150 | for (Connection connection : getDelegate()) { | 161 | for (Connection connection : getDelegate()) { |
| 151 | - status = cache.get(connection); | 162 | + status = getProducer().getStatus(connection); |
| 152 | result = result && status.isMarkedRollback(); | 163 | result = result && status.isMarkedRollback(); |
| 153 | } | 164 | } |
| 154 | 165 | ||
| 155 | return result; | 166 | return result; |
| 156 | } | 167 | } |
| 157 | 168 | ||
| 158 | - private static class Status implements Serializable { | 169 | + /*private static class Status implements Serializable { |
| 159 | 170 | ||
| 160 | private static final long serialVersionUID = 1L; | 171 | private static final long serialVersionUID = 1L; |
| 161 | 172 | ||
| @@ -178,5 +189,5 @@ public class JDBCTransaction implements Transaction { | @@ -178,5 +189,5 @@ public class JDBCTransaction implements Transaction { | ||
| 178 | public void setRollbackOnly(boolean markedRollback) { | 189 | public void setRollbackOnly(boolean markedRollback) { |
| 179 | this.markedRollback = markedRollback; | 190 | this.markedRollback = markedRollback; |
| 180 | } | 191 | } |
| 181 | - } | 192 | + }*/ |
| 182 | } | 193 | } |