From 706d8cba00c5b1d429f5484e535aeae4fbda2064 Mon Sep 17 00:00:00 2001 From: Steve Marion Date: Wed, 31 Jul 2024 11:28:32 +0200 Subject: [PATCH] SONAR-22649 Add support for concurrent usage of dbTester. --- .../testFixtures/java/org/sonar/db/DbTester.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java index c9e8783b46e..8e34db0f454 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/DbTester.java @@ -66,7 +66,7 @@ public class DbTester extends AbstractDbTester { private final System2 system2; private final AuditPersister auditPersister; private DbClient client; - private DbSession session = null; + ThreadLocal session = new ThreadLocal<>(); private final UserDbTester userTester; private final ComponentDbTester componentTester; private final ProjectLinkDbTester componentLinkTester; @@ -250,18 +250,19 @@ public class DbTester extends AbstractDbTester { @Override protected void after() { - if (session != null) { - session.rollback(); - session.close(); + if (session.get() != null) { + session.get().rollback(); + session.get().close(); + session.remove(); } db.stop(); } public DbSession getSession() { - if (session == null) { - session = db.getMyBatis().openSession(false); + if (session.get() == null) { + session.set(db.getMyBatis().openSession(false)); } - return session; + return session.get(); } public void commit() { -- 2.39.5