|
1
|
|
|
package io.mcarle.strix; |
|
2
|
|
|
|
|
3
|
|
|
import org.slf4j.Logger; |
|
4
|
|
|
import org.slf4j.LoggerFactory; |
|
5
|
|
|
|
|
6
|
|
|
import javax.persistence.EntityManager; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Internaly used by strix to bind the entity manager and the persistence unit, to which the entity manager belongs, to |
|
10
|
|
|
* the transactional thread. After the transactional method is finished, strix unbinds everything. |
|
11
|
|
|
*/ |
|
12
|
1 |
|
final class PersistenceManager { |
|
13
|
|
|
|
|
14
|
1 |
|
private static final Logger LOG = LoggerFactory.getLogger(PersistenceManager.class); |
|
15
|
1 |
|
private static final ThreadLocal<String> ENTITY_MANAGER_PERSISTENCE_UNIT_STORE = new ThreadLocal<>(); |
|
16
|
1 |
|
private static final ThreadLocal<EntityManager> ENTITY_MANAGER_STORE = new ThreadLocal<>(); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Bind the used persistence unit and entity manager to the current thread |
|
20
|
|
|
* |
|
21
|
|
|
* @param persistenceUnit Used persistence unit |
|
22
|
|
|
* @param entityManager Used entity manager |
|
23
|
|
|
*/ |
|
24
|
|
|
static void setEntityManager(String persistenceUnit, EntityManager entityManager) { |
|
25
|
1 |
|
LOG.trace("Bind entity manager and persistence unit ({}) to current thread", persistenceUnit); |
|
26
|
1 |
|
ENTITY_MANAGER_PERSISTENCE_UNIT_STORE.set(persistenceUnit); |
|
27
|
1 |
|
ENTITY_MANAGER_STORE.set(entityManager); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Unbinds the entity manager and persistence unit from current thread |
|
32
|
|
|
*/ |
|
33
|
|
|
static void clearEntityManager() { |
|
34
|
1 |
|
LOG.trace("Unbind entity manager and persistence unit from current thread"); |
|
35
|
1 |
|
ENTITY_MANAGER_PERSISTENCE_UNIT_STORE.remove(); |
|
36
|
1 |
|
ENTITY_MANAGER_STORE.remove(); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Checks if there is an entity manager bound to current thread |
|
41
|
|
|
* |
|
42
|
|
|
* @return {@code true}, if an entity manager is bound to the current thread. Otherwise {@code false}. |
|
43
|
|
|
*/ |
|
44
|
|
|
static boolean isEntityManagerPresent() { |
|
45
|
1 |
|
return ENTITY_MANAGER_STORE.get() != null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Checks if there is an entity manager bound to current thread and if it belongs to the delivered persistence unit |
|
50
|
|
|
* |
|
51
|
|
|
* @param persistenceUnit The persistence unit name to which the entity manager should belong to |
|
52
|
|
|
* @return {@code true}, if the entity manager belongs to {@code persistenceUnit}. Otherwise {@code false}. |
|
53
|
|
|
*/ |
|
54
|
|
|
static boolean isEntityManagerFromPU(String persistenceUnit) { |
|
55
|
1 |
|
return persistenceUnit.equals(ENTITY_MANAGER_PERSISTENCE_UNIT_STORE.get()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the entity manager bpund to the current thread. |
|
60
|
|
|
* |
|
61
|
|
|
* @return The entity manager bound to the current thread, or {@code null} if no entity manager is bound. |
|
62
|
|
|
*/ |
|
63
|
|
|
static EntityManager getEntityManager() { |
|
64
|
1 |
|
return ENTITY_MANAGER_STORE.get(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|