forked from santanusinha/dropwizard-db-sharding-bundle
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handling unbinding when nested txns are running on different DB [#161…
…113716]
- Loading branch information
Siddharth Srivastava
committed
Nov 21, 2018
1 parent
0246817
commit 0f8fc58
Showing
12 changed files
with
246 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ore/src/main/java/in/cleartax/dropwizard/sharding/hibernate/MultiTenantSessionSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2018 Saurabh Agrawal (Cleartax) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package in.cleartax.dropwizard.sharding.hibernate; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
import org.hibernate.SessionFactory; | ||
|
||
@AllArgsConstructor | ||
@Value | ||
@Builder | ||
public class MultiTenantSessionSource { | ||
private SessionFactory sessionFactory; | ||
private MultiTenantDataSourceFactory dataSourceFactory; | ||
private MultiTenantUnitOfWorkAwareProxyFactory unitOfWorkAwareProxyFactory; | ||
} |
162 changes: 162 additions & 0 deletions
162
.../src/main/java/in/cleartax/dropwizard/sharding/hibernate/MultiTenantUnitOfWorkAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package in.cleartax.dropwizard.sharding.hibernate; | ||
|
||
import io.dropwizard.hibernate.HibernateBundle; | ||
import io.dropwizard.hibernate.UnitOfWork; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.context.internal.ManagedSessionContext; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Map; | ||
import java.util.Stack; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Created on 14/11/18 | ||
*/ | ||
public class MultiTenantUnitOfWorkAspect { | ||
private static ThreadLocal<Stack<Session>> CONTEXT_OPEN_SESSIONS = ThreadLocal.withInitial(Stack::new); | ||
|
||
private final Map<String, SessionFactory> sessionFactories; | ||
// Context variables | ||
@Nullable | ||
private UnitOfWork unitOfWork; | ||
@Nullable | ||
private Session session; | ||
@Nullable | ||
private SessionFactory sessionFactory; | ||
|
||
public MultiTenantUnitOfWorkAspect(Map<String, SessionFactory> sessionFactories) { | ||
this.sessionFactories = sessionFactories; | ||
} | ||
|
||
public void beforeStart(@Nullable UnitOfWork unitOfWork) { | ||
if (unitOfWork == null) { | ||
return; | ||
} | ||
this.unitOfWork = unitOfWork; | ||
|
||
sessionFactory = sessionFactories.get(unitOfWork.value()); | ||
if (sessionFactory == null) { | ||
// If the user didn't specify the name of a session factory, | ||
// and we have only one registered, we can assume that it's the right one. | ||
if (unitOfWork.value().equals(HibernateBundle.DEFAULT_NAME) && sessionFactories.size() == 1) { | ||
sessionFactory = sessionFactories.values().iterator().next(); | ||
} else { | ||
throw new IllegalArgumentException("Unregistered Hibernate bundle: '" + unitOfWork.value() + "'"); | ||
} | ||
} | ||
session = sessionFactory.openSession(); | ||
assert session != null; | ||
try { | ||
configureSession(); | ||
bind(session); | ||
beginTransaction(unitOfWork, session); | ||
} catch (Throwable th) { | ||
session.close(); | ||
session = null; | ||
unbind(sessionFactory); | ||
throw th; | ||
} | ||
} | ||
|
||
public void afterEnd() { | ||
if (unitOfWork == null || session == null) { | ||
return; | ||
} | ||
|
||
try { | ||
commitTransaction(unitOfWork, session); | ||
} catch (Exception e) { | ||
rollbackTransaction(unitOfWork, session); | ||
throw e; | ||
} | ||
// We should not close the session to let the lazy loading work during serializing a response to the client. | ||
// If the response successfully serialized, then the session will be closed by the `onFinish` method | ||
} | ||
|
||
public void onError() { | ||
if (unitOfWork == null || session == null) { | ||
return; | ||
} | ||
|
||
try { | ||
rollbackTransaction(unitOfWork, session); | ||
} finally { | ||
onFinish(); | ||
} | ||
} | ||
|
||
public void onFinish() { | ||
try { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} finally { | ||
session = null; | ||
unbind(sessionFactory); | ||
} | ||
} | ||
|
||
protected void configureSession() { | ||
checkNotNull(unitOfWork); | ||
checkNotNull(session); | ||
session.setDefaultReadOnly(unitOfWork.readOnly()); | ||
session.setCacheMode(unitOfWork.cacheMode()); | ||
session.setHibernateFlushMode(unitOfWork.flushMode()); | ||
} | ||
|
||
private void beginTransaction(UnitOfWork unitOfWork, Session session) { | ||
if (!unitOfWork.transactional()) { | ||
return; | ||
} | ||
session.beginTransaction(); | ||
} | ||
|
||
private void rollbackTransaction(UnitOfWork unitOfWork, Session session) { | ||
if (!unitOfWork.transactional()) { | ||
return; | ||
} | ||
final Transaction txn = session.getTransaction(); | ||
if (txn != null && txn.getStatus().canRollback()) { | ||
txn.rollback(); | ||
} | ||
} | ||
|
||
private void commitTransaction(UnitOfWork unitOfWork, Session session) { | ||
if (!unitOfWork.transactional()) { | ||
return; | ||
} | ||
final Transaction txn = session.getTransaction(); | ||
if (txn != null && txn.getStatus().canRollback()) { | ||
txn.commit(); | ||
} | ||
} | ||
|
||
protected Session getSession() { | ||
return requireNonNull(session); | ||
} | ||
|
||
protected SessionFactory getSessionFactory() { | ||
return requireNonNull(sessionFactory); | ||
} | ||
|
||
private void bind(Session session) { | ||
CONTEXT_OPEN_SESSIONS.get().push(session); | ||
ManagedSessionContext.bind(session); | ||
} | ||
|
||
private void unbind(SessionFactory sessionFactory) { | ||
ManagedSessionContext.unbind(sessionFactory); | ||
// This defensive check is needed as in case of exception onFinish gets called multiple times. | ||
if (!CONTEXT_OPEN_SESSIONS.get().isEmpty()) { | ||
CONTEXT_OPEN_SESSIONS.get().pop(); | ||
} | ||
if (!CONTEXT_OPEN_SESSIONS.get().isEmpty()) { | ||
ManagedSessionContext.bind(CONTEXT_OPEN_SESSIONS.get().peek()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.