Fix support for dropping local temporary tables

This commit is contained in:
Christian Beikov 2021-12-13 17:33:04 +01:00
parent eecda61ceb
commit c4e7e2b3fe
14 changed files with 48 additions and 44 deletions

View File

@ -330,10 +330,11 @@ public final class ExecuteWithTemporaryTableHelper {
public static void performAfterTemporaryTableUseActions(
TemporaryTable temporaryTable,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
AfterUseAction afterUseAction,
ExecutionContext executionContext) {
final SessionFactoryImplementor factory = executionContext.getSession().getFactory();
final Dialect dialect = factory.getJdbcServices().getDialect();
switch ( dialect.getTemporaryTableAfterUseAction() ) {
switch ( afterUseAction ) {
case CLEAN:
TemporaryTableHelper.cleanTemporaryTableRows(
temporaryTable,

View File

@ -35,6 +35,7 @@ public class GlobalTemporaryTableInsertStrategy extends GlobalTemporaryTableStra
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
// generally a global temp table should already track a Connection-specific uid,
// but just in case a particular env needs it...
session -> session.getSessionIdentifier().toString(),

View File

@ -36,6 +36,7 @@ public class GlobalTemporaryTableMutationStrategy extends GlobalTemporaryTableSt
sqmUpdate,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
// generally a global temp table should already track a Connection-specific uid,
// but just in case a particular env needs it...
session -> session.getSessionIdentifier().toString(),
@ -52,6 +53,7 @@ public class GlobalTemporaryTableMutationStrategy extends GlobalTemporaryTableSt
sqmDelete,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
// generally a global temp table should already track a Connection-specific uid,
// but just in case a particular env needs it...
session -> session.getSessionIdentifier().toString(),

View File

@ -79,6 +79,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
private final SqmInsertStatement<?> sqmInsert;
private final MultiTableSqmMutationConverter sqmConverter;
private final TemporaryTable entityTable;
private final AfterUseAction afterUseAction;
private final Function<SharedSessionContractImplementor, String> sessionUidAccess;
private final DomainParameterXref domainParameterXref;
private final TableGroup updatingTableGroup;
@ -96,6 +97,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
SqmInsertStatement<?> sqmInsert,
MultiTableSqmMutationConverter sqmConverter,
TemporaryTable entityTable,
AfterUseAction afterUseAction,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
DomainParameterXref domainParameterXref,
TableGroup insertingTableGroup,
@ -108,6 +110,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
this.sqmInsert = sqmInsert;
this.sqmConverter = sqmConverter;
this.entityTable = entityTable;
this.afterUseAction = afterUseAction;
this.sessionUidAccess = sessionUidAccess;
this.domainParameterXref = domainParameterXref;
this.updatingTableGroup = insertingTableGroup;
@ -202,6 +205,7 @@ public class InsertExecutionDelegate implements TableBasedInsertHandler.Executio
ExecuteWithTemporaryTableHelper.performAfterTemporaryTableUseActions(
entityTable,
sessionUidAccess,
afterUseAction,
executionContext
);
}

View File

@ -35,6 +35,9 @@ public class LocalTemporaryTableInsertStrategy extends LocalTemporaryTableStrate
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
isDropIdTables()
? AfterUseAction.DROP
: getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> {
throw new UnsupportedOperationException( "Unexpected call to access Session uid" );
},

View File

@ -36,6 +36,9 @@ public class LocalTemporaryTableMutationStrategy extends LocalTemporaryTableStra
sqmUpdate,
domainParameterXref,
getTemporaryTable(),
isDropIdTables()
? AfterUseAction.DROP
: getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> {
throw new UnsupportedOperationException( "Unexpected call to access Session uid" );
},
@ -52,6 +55,9 @@ public class LocalTemporaryTableMutationStrategy extends LocalTemporaryTableStra
sqmDelete,
domainParameterXref,
getTemporaryTable(),
isDropIdTables()
? AfterUseAction.DROP
: getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> {
throw new UnsupportedOperationException( "Unexpected call to access Session uid" );
},

View File

@ -55,52 +55,18 @@ public class LocalTemporaryTableStrategy {
);
}
public void release(
SessionFactoryImplementor sessionFactory,
JdbcConnectionAccess connectionAccess) {
if ( !dropIdTables ) {
return;
}
dropIdTables = false;
final TemporaryTable temporaryTable = getTemporaryTable();
log.debugf( "Dropping local-temp ID table : %s", temporaryTable.getTableExpression() );
final TemporaryTableHelper.TemporaryTableDropWork temporaryTableDropWork = new TemporaryTableHelper.TemporaryTableDropWork(
temporaryTable,
sessionFactory
);
Connection connection;
try {
connection = connectionAccess.obtainConnection();
}
catch (UnsupportedOperationException e) {
// assume this comes from org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl
log.debugf( "Unable to obtain JDBC connection; unable to drop local-temp ID table : %s", temporaryTable.getTableExpression() );
return;
}
catch (SQLException e) {
log.error( "Unable obtain JDBC Connection", e );
return;
}
try {
temporaryTableDropWork.execute( connection );
}
finally {
try {
connectionAccess.releaseConnection( connection );
}
catch (SQLException ignore) {
}
}
public void release(SessionFactoryImplementor sessionFactory, JdbcConnectionAccess connectionAccess) {
// Nothing to do here. This happens through ExecuteWithTemporaryTableHelper.performAfterTemporaryTableUseActions
}
public TemporaryTable getTemporaryTable() {
return temporaryTable;
}
public boolean isDropIdTables() {
return dropIdTables;
}
public SessionFactoryImplementor getSessionFactory() {
return sessionFactory;
}

View File

@ -44,6 +44,7 @@ public class PersistentTableInsertStrategy extends PersistentTableStrategy imple
sqmInsertStatement,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> session.getSessionIdentifier().toString(),
getSessionFactory()
).execute( context );

View File

@ -38,6 +38,7 @@ public class PersistentTableMutationStrategy extends PersistentTableStrategy imp
sqmUpdate,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> session.getSessionIdentifier().toString(),
getSessionFactory()
).execute( context );
@ -52,6 +53,7 @@ public class PersistentTableMutationStrategy extends PersistentTableStrategy imp
sqmDelete,
domainParameterXref,
getTemporaryTable(),
getSessionFactory().getJdbcServices().getDialect().getTemporaryTableAfterUseAction(),
session -> session.getSessionIdentifier().toString(),
getSessionFactory()
).execute( context );

View File

@ -70,6 +70,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
private final EntityMappingType entityDescriptor;
private final TemporaryTable idTable;
private final AfterUseAction afterUseAction;
private final SqmDeleteStatement<?> sqmDelete;
private final DomainParameterXref domainParameterXref;
private final SessionFactoryImplementor sessionFactory;
@ -81,6 +82,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
public RestrictedDeleteExecutionDelegate(
EntityMappingType entityDescriptor,
TemporaryTable idTable,
AfterUseAction afterUseAction,
SqmDeleteStatement<?> sqmDelete,
DomainParameterXref domainParameterXref,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
@ -90,6 +92,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
SessionFactoryImplementor sessionFactory) {
this.entityDescriptor = entityDescriptor;
this.idTable = idTable;
this.afterUseAction = afterUseAction;
this.sqmDelete = sqmDelete;
this.domainParameterXref = domainParameterXref;
this.sessionUidAccess = sessionUidAccess;
@ -488,6 +491,7 @@ public class RestrictedDeleteExecutionDelegate implements TableBasedDeleteHandle
ExecuteWithTemporaryTableHelper.performAfterTemporaryTableUseActions(
idTable,
sessionUidAccess,
afterUseAction,
executionContext
);
}

View File

@ -32,8 +32,8 @@ public class TableBasedDeleteHandler
}
private final TemporaryTable idTable;
private final AfterUseAction afterUseAction;
private final Function<SharedSessionContractImplementor,String> sessionUidAccess;
private final DomainParameterXref domainParameterXref;
@ -41,12 +41,14 @@ public class TableBasedDeleteHandler
SqmDeleteStatement<?> sqmDeleteStatement,
DomainParameterXref domainParameterXref,
TemporaryTable idTable,
AfterUseAction afterUseAction,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
SessionFactoryImplementor sessionFactory) {
super( sqmDeleteStatement, sessionFactory );
this.idTable = idTable;
this.domainParameterXref = domainParameterXref;
this.afterUseAction = afterUseAction;
this.sessionUidAccess = sessionUidAccess;
}
@ -66,9 +68,10 @@ public class TableBasedDeleteHandler
return new RestrictedDeleteExecutionDelegate(
getEntityDescriptor(),
idTable,
getSqmDeleteOrUpdateStatement(),
afterUseAction, getSqmDeleteOrUpdateStatement(),
domainParameterXref,
sessionUidAccess,
executionContext.getQueryOptions(),
executionContext.getSession().getLoadQueryInfluencers(),
executionContext.getQueryParameterBindings(),

View File

@ -68,6 +68,7 @@ public class TableBasedInsertHandler implements InsertHandler {
private final SessionFactoryImplementor sessionFactory;
private final TemporaryTable entityTable;
private final AfterUseAction afterUseAction;
private final Function<SharedSessionContractImplementor,String> sessionUidAccess;
private final DomainParameterXref domainParameterXref;
@ -77,9 +78,11 @@ public class TableBasedInsertHandler implements InsertHandler {
SqmInsertStatement<?> sqmInsert,
DomainParameterXref domainParameterXref,
TemporaryTable entityTable,
AfterUseAction afterUseAction,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
SessionFactoryImplementor sessionFactory) {
this.sqmInsertStatement = sqmInsert;
this.afterUseAction = afterUseAction;
this.sessionFactory = sessionFactory;
this.entityTable = entityTable;
this.sessionUidAccess = sessionUidAccess;
@ -262,6 +265,7 @@ public class TableBasedInsertHandler implements InsertHandler {
sqmInsertStatement,
converterDelegate,
entityTable,
afterUseAction,
sessionUidAccess,
domainParameterXref,
insertingTableGroup,

View File

@ -58,6 +58,7 @@ public class TableBasedUpdateHandler
}
private final TemporaryTable idTable;
private final AfterUseAction afterUseAction;
private final Function<SharedSessionContractImplementor,String> sessionUidAccess;
private final DomainParameterXref domainParameterXref;
@ -67,10 +68,12 @@ public class TableBasedUpdateHandler
SqmUpdateStatement<?> sqmUpdate,
DomainParameterXref domainParameterXref,
TemporaryTable idTable,
AfterUseAction afterUseAction,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
SessionFactoryImplementor sessionFactory) {
super( sqmUpdate, sessionFactory );
this.idTable = idTable;
this.afterUseAction = afterUseAction;
this.sessionUidAccess = sessionUidAccess;
this.domainParameterXref = domainParameterXref;
@ -207,7 +210,7 @@ public class TableBasedUpdateHandler
getSqmUpdate(),
converterDelegate,
idTable,
sessionUidAccess,
afterUseAction, sessionUidAccess,
domainParameterXref,
updatingTableGroup,
hierarchyRootTableReference,

View File

@ -50,6 +50,7 @@ public class UpdateExecutionDelegate implements TableBasedUpdateHandler.Executio
private final SqmUpdateStatement sqmUpdate;
private final MultiTableSqmMutationConverter sqmConverter;
private final TemporaryTable idTable;
private final AfterUseAction afterUseAction;
private final Function<SharedSessionContractImplementor, String> sessionUidAccess;
private final DomainParameterXref domainParameterXref;
private final TableGroup updatingTableGroup;
@ -67,6 +68,7 @@ public class UpdateExecutionDelegate implements TableBasedUpdateHandler.Executio
SqmUpdateStatement<?> sqmUpdate,
MultiTableSqmMutationConverter sqmConverter,
TemporaryTable idTable,
AfterUseAction afterUseAction,
Function<SharedSessionContractImplementor, String> sessionUidAccess,
DomainParameterXref domainParameterXref,
TableGroup updatingTableGroup,
@ -80,6 +82,7 @@ public class UpdateExecutionDelegate implements TableBasedUpdateHandler.Executio
this.sqmUpdate = sqmUpdate;
this.sqmConverter = sqmConverter;
this.idTable = idTable;
this.afterUseAction = afterUseAction;
this.sessionUidAccess = sessionUidAccess;
this.domainParameterXref = domainParameterXref;
this.updatingTableGroup = updatingTableGroup;
@ -180,6 +183,7 @@ public class UpdateExecutionDelegate implements TableBasedUpdateHandler.Executio
ExecuteWithTemporaryTableHelper.performAfterTemporaryTableUseActions(
idTable,
sessionUidAccess,
afterUseAction,
executionContext
);
}