Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.grails.orm.hibernate;

import java.util.List;

import javax.sql.DataSource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.engine.jdbc.spi.JdbcCoordinator;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.resource.jdbc.spi.LogicalConnectionImplementor;

import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.transaction.support.TransactionSynchronization;

import org.grails.orm.hibernate.support.hibernate7.SessionHolder;
import org.grails.orm.hibernate.support.hibernate7.TransactionResources;

/**
* Base strategy handling symmetric capture, unbinding, and restoration of session and connection holders
* as well as Spring transaction synchronizations.
*/
abstract class AbstractOuterSessionLifecycleStrategy implements OuterSessionLifecycleStrategy {

protected final SessionFactory sessionFactory;
protected final DataSource dataSource;
protected final TransactionResources txResources;
protected final SessionHolder previousHolder;
protected final boolean lazyConnection;

protected boolean wasPhysicallyConnectedAtPrepare = false;
protected ConnectionHolder previousConnectionHolder;
protected boolean previousActiveSynchronization;
protected List<TransactionSynchronization> transactionSynchronizations;

AbstractOuterSessionLifecycleStrategy(
SessionFactory sessionFactory,
DataSource dataSource,
TransactionResources txResources,
SessionHolder previousHolder,
boolean lazyConnection) {
this.sessionFactory = sessionFactory;
this.dataSource = dataSource;
this.txResources = txResources;
this.previousHolder = previousHolder;
this.lazyConnection = lazyConnection;
}

protected LogicalConnectionImplementor getLogicalConnection() {
if (previousHolder == null || previousHolder.getSession() == null || !previousHolder.getSession().isOpen()) {
return null;
}

Session previousSession = previousHolder.getSession();
if (!(previousSession instanceof SessionImplementor sessionImpl)) {
return null;
}

JdbcCoordinator jdbcCoordinator = sessionImpl.getJdbcCoordinator();
if (jdbcCoordinator == null) {
return null;
}

return jdbcCoordinator.getLogicalConnection();
}

protected void disconnectIfConnected() {
LogicalConnectionImplementor logicalConnection = getLogicalConnection();
if (logicalConnection != null && logicalConnection.isOpen() && logicalConnection.isPhysicallyConnected()) {
logicalConnection.manualDisconnect();
}
}

@Override
public void prepare() {
LogicalConnectionImplementor logicalConnection = getLogicalConnection();
if (logicalConnection != null) {
this.wasPhysicallyConnectedAtPrepare = logicalConnection.isPhysicallyConnected();
}
}

@Override
public void suspendAndUnbind() {
previousConnectionHolder = dataSource != null ? (ConnectionHolder) txResources.getResource(dataSource) : null;
previousActiveSynchronization = txResources.isSynchronizationActive();
transactionSynchronizations = previousActiveSynchronization ? txResources.getSynchronizations() : null;

if (previousActiveSynchronization) {
txResources.clearSynchronization();
txResources.initSynchronization();
}

if (previousHolder != null) {
txResources.unbindResource(sessionFactory);
}

if (previousConnectionHolder != null && dataSource != null) {
txResources.unbindResource(dataSource);
}
}

@Override
public void restore() {
if (previousActiveSynchronization) {
txResources.initSynchronization();
if (transactionSynchronizations != null) {
for (TransactionSynchronization transactionSynchronization : transactionSynchronizations) {
txResources.registerSynchronization(transactionSynchronization);
}
}
}

if (previousHolder != null) {
txResources.bindResource(sessionFactory, previousHolder);
if (!wasPhysicallyConnectedAtPrepare && (!lazyConnection || !previousActiveSynchronization)) {
disconnectIfConnected();
}
}

if (previousConnectionHolder != null && dataSource != null) {
txResources.bindResource(dataSource, previousConnectionHolder);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.grails.orm.hibernate;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;

import org.grails.orm.hibernate.support.hibernate7.SessionHolder;
import org.grails.orm.hibernate.support.hibernate7.TransactionResources;

/**
* Strategy applied when the outer session is bound to an active write-bearing physical transaction
* (such as {@code PROPAGATION_REQUIRED} or {@code PROPAGATION_MANDATORY}).
*
* <p>Invariant: Never disconnect an active outer transaction during beforeCompletion or suspension,
* as doing so drops uncommitted entity dirty checks and pending flushes.</p>
*/
class ActiveTransactionOuterSessionStrategy extends AbstractOuterSessionLifecycleStrategy {

ActiveTransactionOuterSessionStrategy(
SessionFactory sessionFactory,
DataSource dataSource,
TransactionResources txResources,
SessionHolder previousHolder,
boolean lazyConnection) {
super(sessionFactory, dataSource, txResources, previousHolder, lazyConnection);
}

@Override
public void prepare() {
this.wasPhysicallyConnectedAtPrepare = true;
// No preparation needed: the active transaction already manages its physical connection.
// We explicitly do NOT disconnect or register disconnect synchronizations here.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* https://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 org.grails.orm.hibernate;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;

import org.grails.orm.hibernate.support.hibernate7.SessionHolder;
import org.grails.orm.hibernate.support.hibernate7.TransactionResources;

/**
* Strategy applied when an outer session exists but no Spring transaction synchronization is active.
*
* <p>Unbinds the outer session and connection holders while the new session runs and rebinds them afterwards.
* It does not prime the outer session, so an idle session never checks out a pooled connection. If the outer
* session was not connected on entry and is connected when the scope ends, it is disconnected on restore so
* that connection returns to the pool. A bound holder whose session is closed is unbound and rebound the same way.</p>
*/
class DefaultOuterSessionStrategy extends AbstractOuterSessionLifecycleStrategy {

DefaultOuterSessionStrategy(
SessionFactory sessionFactory,
DataSource dataSource,
TransactionResources txResources,
SessionHolder previousHolder,
boolean lazyConnection) {
super(sessionFactory, dataSource, txResources, previousHolder, lazyConnection);
}

@Override
public void prepare() {
super.prepare();
}
}
Loading
Loading