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 | 37 | private ResourceBundle bundle; |
38 | 38 | |
39 | 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 | 42 | @Inject |
42 | 43 | private DataSourceProducer producer; |
... | ... | @@ -79,6 +80,7 @@ public class ConnectionProducer implements Serializable { |
79 | 80 | disableAutoCommit(connection); |
80 | 81 | |
81 | 82 | cache.put(name, connection); |
83 | + statusCache.put(connection, new Status()); | |
82 | 84 | logger.info(bundle.getString("connection-was-created", name)); |
83 | 85 | |
84 | 86 | } catch (Exception cause) { |
... | ... | @@ -150,5 +152,34 @@ public class ConnectionProducer implements Serializable { |
150 | 152 | public Map<String, Connection> getCache() { |
151 | 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 | 27 | private static final long serialVersionUID = 1L; |
28 | 28 | |
29 | 29 | private final String dataSourceName; |
30 | - | |
30 | + | |
31 | 31 | public ConnectionProxy(String dataSourceName) { |
32 | 32 | this.dataSourceName = dataSourceName; |
33 | 33 | } |
... | ... | @@ -236,4 +236,5 @@ public class ConnectionProxy implements Connection, Serializable { |
236 | 236 | public <T> T unwrap(Class<T> iface) throws SQLException { |
237 | 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 | 38 | |
39 | 39 | import static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY; |
40 | 40 | |
41 | -import java.io.Serializable; | |
42 | 41 | import java.sql.Connection; |
43 | 42 | import java.util.Collection; |
44 | -import java.util.Collections; | |
45 | -import java.util.HashMap; | |
46 | -import java.util.Map; | |
47 | 43 | |
48 | 44 | import br.gov.frameworkdemoiselle.DemoiselleException; |
49 | 45 | import br.gov.frameworkdemoiselle.annotation.Priority; |
50 | 46 | import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer; |
47 | +import br.gov.frameworkdemoiselle.internal.producer.ConnectionProducer.Status; | |
51 | 48 | import br.gov.frameworkdemoiselle.util.Beans; |
52 | 49 | |
50 | + | |
53 | 51 | /** |
54 | 52 | * Represents the strategy destinated to manage JDBC transactions. |
55 | 53 | * |
... | ... | @@ -63,17 +61,22 @@ public class JDBCTransaction implements Transaction { |
63 | 61 | |
64 | 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 | 67 | private ConnectionProducer getProducer() { |
69 | 68 | if (producer == null) { |
70 | 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 | 82 | return producer; |
... | ... | @@ -87,7 +90,7 @@ public class JDBCTransaction implements Transaction { |
87 | 90 | public void begin() { |
88 | 91 | Status status; |
89 | 92 | for (Connection connection : getDelegate()) { |
90 | - status = cache.get(connection); | |
93 | + status = getProducer().getStatus(connection); | |
91 | 94 | status.setActive(true); |
92 | 95 | } |
93 | 96 | } |
... | ... | @@ -97,9 +100,13 @@ public class JDBCTransaction implements Transaction { |
97 | 100 | */ |
98 | 101 | @Override |
99 | 102 | public void commit() { |
103 | + Status status; | |
104 | + | |
100 | 105 | for (Connection connection : getDelegate()) { |
101 | 106 | try { |
102 | 107 | connection.commit(); |
108 | + status = getProducer().getStatus(connection); | |
109 | + status.setActive(false); | |
103 | 110 | } catch (Exception cause) { |
104 | 111 | throw new DemoiselleException(cause); |
105 | 112 | } |
... | ... | @@ -111,9 +118,13 @@ public class JDBCTransaction implements Transaction { |
111 | 118 | */ |
112 | 119 | @Override |
113 | 120 | public void rollback() { |
121 | + Status status; | |
122 | + | |
114 | 123 | for (Connection connection : getDelegate()) { |
115 | 124 | try { |
116 | 125 | connection.rollback(); |
126 | + status = getProducer().getStatus(connection); | |
127 | + status.setActive(false); | |
117 | 128 | } catch (Exception cause) { |
118 | 129 | throw new DemoiselleException(cause); |
119 | 130 | } |
... | ... | @@ -124,7 +135,7 @@ public class JDBCTransaction implements Transaction { |
124 | 135 | public void setRollbackOnly() { |
125 | 136 | Status status; |
126 | 137 | for (Connection connection : getDelegate()) { |
127 | - status = cache.get(connection); | |
138 | + status = getProducer().getStatus(connection); | |
128 | 139 | status.setRollbackOnly(true); |
129 | 140 | } |
130 | 141 | } |
... | ... | @@ -135,7 +146,7 @@ public class JDBCTransaction implements Transaction { |
135 | 146 | boolean result = true; |
136 | 147 | |
137 | 148 | for (Connection connection : getDelegate()) { |
138 | - status = cache.get(connection); | |
149 | + status = getProducer().getStatus(connection); | |
139 | 150 | result = result && status.isActive(); |
140 | 151 | } |
141 | 152 | |
... | ... | @@ -148,14 +159,14 @@ public class JDBCTransaction implements Transaction { |
148 | 159 | boolean result = true; |
149 | 160 | |
150 | 161 | for (Connection connection : getDelegate()) { |
151 | - status = cache.get(connection); | |
162 | + status = getProducer().getStatus(connection); | |
152 | 163 | result = result && status.isMarkedRollback(); |
153 | 164 | } |
154 | 165 | |
155 | 166 | return result; |
156 | 167 | } |
157 | 168 | |
158 | - private static class Status implements Serializable { | |
169 | + /*private static class Status implements Serializable { | |
159 | 170 | |
160 | 171 | private static final long serialVersionUID = 1L; |
161 | 172 | |
... | ... | @@ -178,5 +189,5 @@ public class JDBCTransaction implements Transaction { |
178 | 189 | public void setRollbackOnly(boolean markedRollback) { |
179 | 190 | this.markedRollback = markedRollback; |
180 | 191 | } |
181 | - } | |
192 | + }*/ | |
182 | 193 | } | ... | ... |