BrightSide Workbench Full Report + Source Code
DaoTransaction.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa;
20 
21 import java.io.Closeable;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.persistence.EntityTransaction;
26 import javax.persistence.Query;
27 import javax.persistence.criteria.CriteriaDelete;
28 import javax.persistence.criteria.CriteriaQuery;
29 import javax.persistence.criteria.CriteriaUpdate;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.exception.ElephantException;
32 import org.turro.util.AtomicCounter;
33 
38 public class DaoTransaction implements Closeable {
39 
40  private final DaoManager manager;
41  private final EntityTransaction transaction;
42 
43  public DaoTransaction(Dao dao) {
44  this.manager = new DaoManager(dao);
45  this.transaction = manager.getManager().getTransaction();
46  startTransaction();
47  }
48 
49  /* DaoManager delegates */
50 
51  public <T> T saveObject(T obj) {
52  return manager.saveObject(obj);
53  }
54 
55  public void persist(Object obj) {
56  manager.persist(obj);
57  }
58 
59  public <T> T find(Class<T> javaClass, Object arg) {
60  return manager.find(javaClass, arg);
61  }
62 
63  public void remove(Object obj) {
64  manager.remove(saveObject(obj));
65  }
66 
67  public <T> Query createQuery(CriteriaQuery<T> query) {
68  return manager.createQuery(query);
69  }
70 
71  public <T> Query createQuery(String clause, Class<T> javaClass) {
72  return manager.createQuery(clause, javaClass);
73  }
74 
75  public Query createQuery(String clause) {
76  return manager.createQuery(clause);
77  }
78 
79  public Query createQuery(CriteriaDelete delete) {
80  return manager.createQuery(delete);
81  }
82 
83  public Query createQuery(CriteriaUpdate update) {
84  return manager.createQuery(update);
85  }
86 
87  public Query createNativeQuery(String clause, Class resultClass) {
88  return manager.createNativeQuery(clause, resultClass);
89  }
90 
91  public Query createNativeQuery(String clause) {
92  return manager.createNativeQuery(clause);
93  }
94 
95  public void setNamedParameters(Query q, Map<String, Object> parameters) {
96  manager.setNamedParameters(q, parameters);
97  }
98 
99  public Dao getDao() {
100  return manager.getDao();
101  }
102 
103  /* End delegates */
104 
105  @Override
106  public void close() {
107  if(transaction != null && transaction.isActive()) {
108  try {
109  transaction.commit();
110  } catch (Exception ex) {
111  Logger.getLogger(DaoTransaction.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
112  try {
113  transaction.rollback();
114  } catch (Exception e) {
115  throw new ElephantException(e);
116  }
117  } finally {
118  manager.close();
119  openTransactions.decrement();
120  }
121  } else {
122  manager.close();
123  }
124  }
125 
126  private void startTransaction() {
127  if(transaction != null && !transaction.isActive()) {
128  transaction.begin();
129  openTransactions.increment();
130  }
131  }
132 
133  /* Debug */
134 
135  private static final AtomicCounter openTransactions = new AtomicCounter();
136 
137  public static AtomicCounter getCounter() {
138  return openTransactions;
139  }
140 
141 }
void persist(Object obj)
Definition: DaoManager.java:58
Query createNativeQuery(String clause, Class resultClass)
Definition: DaoManager.java:94
void setNamedParameters(Query q, Map< String, Object > parameters)
EntityManager getManager()
void remove(Object obj)
Definition: DaoManager.java:70
Query createQuery(String clause)
void setNamedParameters(Query q, Map< String, Object > parameters)
Query createNativeQuery(String clause)
Query createQuery(CriteriaDelete delete)
Query createNativeQuery(String clause, Class resultClass)
static AtomicCounter getCounter()
Query createQuery(CriteriaUpdate update)