Package: LoggingJpaTransactionManager
LoggingJpaTransactionManager
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LoggingJpaTransactionManager(EntityManagerFactory) |
|
|
|
|
|
||||||||||||||||||||
doBegin(Object, TransactionDefinition) |
|
|
|
|
|
||||||||||||||||||||
doCommit(DefaultTransactionStatus) |
|
|
|
|
|
||||||||||||||||||||
doRollback(DefaultTransactionStatus) |
|
|
|
|
|
||||||||||||||||||||
getCommitCount() |
|
|
|
|
|
||||||||||||||||||||
getRollbackCount() |
|
|
|
|
|
||||||||||||||||||||
resetCounters() |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * SolidBlue 3: Data safety
5: * http://tidalwave.it/projects/solidblue3
6: *
7: * Copyright (C) 2023 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/solidblue3j-src
23: * git clone https://github.com/tidalwave-it/solidblue3j-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util.spring.jpa.impl;
28:
29: import javax.annotation.Nonnegative;
30: import jakarta.annotation.Nonnull;
31: import java.util.concurrent.atomic.AtomicInteger;
32: import java.io.Serial;
33: import jakarta.persistence.EntityManagerFactory;
34: import org.springframework.beans.factory.annotation.Qualifier;
35: import org.springframework.orm.jpa.JpaTransactionManager;
36: import org.springframework.stereotype.Component;
37: import org.springframework.transaction.TransactionDefinition;
38: import org.springframework.transaction.support.DefaultTransactionStatus;
39: import lombok.NoArgsConstructor;
40: import lombok.extern.slf4j.Slf4j;
41:
42: /***********************************************************************************************************************
43: *
44: * A specialisation of Spring {@link JpaTransactionManager} that logs transaction demarcation and exposes some basic
45: * metrics about transactions.
46: *
47: * @author Fabrizio Giudici
48: *
49: **********************************************************************************************************************/
50: @Component @Qualifier("transactionManager")
51: @NoArgsConstructor @Slf4j
52: public class LoggingJpaTransactionManager extends JpaTransactionManager
53: {
54: @Serial private static final long serialVersionUID = 0L;
55:
56: private final AtomicInteger commitCount = new AtomicInteger();
57:
58: private final AtomicInteger rollbackCount = new AtomicInteger();
59:
60: /*******************************************************************************************************************
61: *
62: ******************************************************************************************************************/
63: public LoggingJpaTransactionManager (@Nonnull final EntityManagerFactory emf)
64: {
65: super(emf);
66: }
67:
68: /*******************************************************************************************************************
69: *
70: * Returns the count of performed commits.
71: *
72: * @return the count of commits
73: *
74: ******************************************************************************************************************/
75: @Nonnegative
76: public int getCommitCount()
77: {
78: return commitCount.intValue();
79: }
80:
81: /*******************************************************************************************************************
82: *
83: * Returns the count of performed rollbacks.
84: *
85: * @return the count of rollbacks
86: *
87: ******************************************************************************************************************/
88: @Nonnegative
89: public int getRollbackCount()
90: {
91: return rollbackCount.intValue();
92: }
93:
94: /*******************************************************************************************************************
95: *
96: * Resets the commit/rollback counters. For tests only.
97: *
98: ******************************************************************************************************************/
99: public void resetCounters()
100: {
101: commitCount.set(0);
102: rollbackCount.set(0);
103: }
104:
105: /*******************************************************************************************************************
106: * {@inheritDoc}
107: ******************************************************************************************************************/
108: @Override
109: protected void doBegin (@Nonnull final Object transaction, @Nonnull final TransactionDefinition definition)
110: {
111: log.info("SQL: BEGIN - tx definition: {}", definition);
112: super.doBegin(transaction, definition);
113: }
114:
115: /*******************************************************************************************************************
116: * {@inheritDoc}
117: ******************************************************************************************************************/
118: @Override
119: protected void doCommit (@Nonnull final DefaultTransactionStatus status)
120: {
121: log.info("SQL: COMMIT");
122: super.doCommit(status);
123: commitCount.incrementAndGet();
124: }
125:
126: /*******************************************************************************************************************
127: * {@inheritDoc}
128: ******************************************************************************************************************/
129: @Override
130: protected void doRollback (@Nonnull final DefaultTransactionStatus status)
131: {
132: log.warn("SQL: ROLLBACK");
133: super.doRollback(status);
134: rollbackCount.incrementAndGet();
135: }
136: }