rsanchez
2015-01-28 09f0f86d50933ea11eb3315e5728718e23d37dcf
securis/src/main/java/net/curisit/securis/db/common/SystemParams.java
....@@ -3,12 +3,12 @@
33 import java.util.Date;
44
55 import javax.enterprise.context.ApplicationScoped;
6
+import javax.inject.Inject;
67 import javax.persistence.EntityManager;
7
-import javax.persistence.PersistenceContext;
8
-import javax.transaction.Transactional;
98
109 import net.curisit.integrity.commons.Utils;
1110 import net.curisit.securis.db.Settings;
11
+import net.curisit.securis.ioc.EntityManagerProvider;
1212
1313 import org.apache.logging.log4j.LogManager;
1414 import org.apache.logging.log4j.Logger;
....@@ -19,8 +19,8 @@
1919 @SuppressWarnings("unused")
2020 private static final Logger LOG = LogManager.getLogger(SystemParams.class);
2121
22
- @PersistenceContext(unitName = "localdb")
23
- private EntityManager em;
22
+ @Inject
23
+ private EntityManagerProvider emProvider;
2424
2525 /**
2626 * Returns the system parameter value for given key
....@@ -109,6 +109,7 @@
109109 * @return
110110 */
111111 public String getParam(String key, String defaultValue) {
112
+ EntityManager em = emProvider.getEntityManager();
112113 Settings p = em.find(Settings.class, key);
113114 return p == null ? defaultValue : p.getValue();
114115 }
....@@ -120,19 +121,26 @@
120121 * @param defaultValue
121122 * @return
122123 */
123
- @Transactional
124124 public void setParam(String key, String value) {
125
- Settings p = em.find(Settings.class, key);
126
- if (p == null) {
127
- p = new Settings();
128
- p.setKey(key);
129
- p.setValue(value);
130
- em.persist(p);
131
- } else {
132
- p.setValue(value);
133
- em.merge(p);
125
+ EntityManager em = emProvider.getEntityManager();
126
+ em.getTransaction().begin();
127
+ try {
128
+ Settings p = em.find(Settings.class, key);
129
+
130
+ if (p == null) {
131
+ p = new Settings();
132
+ p.setKey(key);
133
+ p.setValue(value);
134
+ em.persist(p);
135
+ } else {
136
+ p.setValue(value);
137
+ em.merge(p);
138
+ }
139
+ em.flush();
140
+ em.getTransaction().commit();
141
+ } finally {
142
+ em.getTransaction().rollback();
134143 }
135
- em.flush();
136144 }
137145
138146 /**
....@@ -181,11 +189,17 @@
181189 * @param key
182190 * @return
183191 */
184
- @Transactional
185192 public void removeParam(String key) {
186
- Settings p = em.find(Settings.class, key);
187
- if (p != null) {
188
- em.remove(p);
193
+ EntityManager em = emProvider.getEntityManager();
194
+ em.getTransaction().begin();
195
+ try {
196
+ Settings p = em.find(Settings.class, key);
197
+ if (p != null) {
198
+ em.remove(p);
199
+ }
200
+ em.getTransaction().commit();
201
+ } finally {
202
+ em.getTransaction().rollback();
189203 }
190204 }
191205