Skip to content

Commit c942210

Browse files
committed
wire in proper logging and exception conversion
1 parent 304299f commit c942210

File tree

7 files changed

+52
-70
lines changed

7 files changed

+52
-70
lines changed

hibernate-core/src/main/java/org/hibernate/id/enhanced/ResyncHelper.java

Lines changed: 30 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,55 @@
44
*/
55
package org.hibernate.id.enhanced;
66

7-
import org.hibernate.HibernateException;
8-
import org.hibernate.dialect.Dialect;
7+
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
98

10-
import java.sql.Connection;
11-
import java.sql.PreparedStatement;
129
import java.sql.SQLException;
1310

1411
/**
1512
* @author Gavin King
1613
*/
1714
class ResyncHelper {
1815

19-
// TODO: Use SqlExceptionHelper, SqlStatementLogger, available in the JdbcContext
20-
21-
private static long resultValue(PreparedStatement select) throws SQLException {
22-
try ( var resultSet = select.executeQuery() ) {
23-
resultSet.next();
24-
return resultSet.getLong( 1 );
25-
}
26-
}
27-
28-
static long getNextSequenceValue(Connection connection, String sequenceName, Dialect dialect) {
29-
final String sequenceCurrentValue =
30-
dialect.getSequenceSupport()
31-
.getSequenceNextValString( sequenceName );
32-
try ( var select = connection.prepareStatement( sequenceCurrentValue ) ) {
33-
return resultValue( select );
16+
private static long execute(DdlTransactionIsolator isolator, String sequenceCurrentValue, String message) {
17+
isolator.getJdbcContext().getSqlStatementLogger().logStatement( sequenceCurrentValue );
18+
try ( var select = isolator.getIsolatedConnection().prepareStatement( sequenceCurrentValue ) ) {
19+
try ( var resultSet = select.executeQuery() ) {
20+
resultSet.next();
21+
return resultSet.getLong( 1 );
22+
}
3423
}
3524
catch (SQLException e) {
36-
throw new HibernateException( "Could not fetch the current sequence value from the database", e );
25+
throw isolator.getJdbcContext().getSqlExceptionHelper()
26+
.convert( e, message, sequenceCurrentValue );
3727
}
3828
}
3929

40-
static long getMaxPrimaryKey(Connection connection, String primaryKeyColumnName, String tableName) {
41-
final String selectMax =
42-
"select max(" + primaryKeyColumnName + ") from " + tableName;
43-
try ( var select = connection.prepareStatement( selectMax ) ) {
44-
return resultValue( select );
45-
}
46-
catch (SQLException e) {
47-
throw new HibernateException( "Could not fetch the max primary key from the database", e );
48-
}
30+
static long getNextSequenceValue(DdlTransactionIsolator isolator, String sequenceName) {
31+
return execute( isolator,
32+
isolator.getJdbcContext().getDialect().getSequenceSupport()
33+
.getSequenceNextValString( sequenceName ),
34+
"Could not fetch the current sequence value from the database" );
4935
}
5036

51-
static long getCurrentTableValue(Connection connection, String tableName, String columnName) {
52-
final String selectCurrent =
53-
"select " + columnName + " from " + tableName;
54-
try ( var select = connection.prepareStatement( selectCurrent ) ) {
55-
return resultValue( select );
56-
}
57-
catch (SQLException e) {
58-
throw new HibernateException( "Could not fetch the current table value from the database", e );
59-
}
37+
static long getMaxPrimaryKey(DdlTransactionIsolator isolator, String primaryKeyColumnName, String tableName) {
38+
return execute( isolator,
39+
"select max(" + primaryKeyColumnName + ") from " + tableName,
40+
"Could not fetch the max primary key from the database" );
41+
}
42+
43+
static long getCurrentTableValue(DdlTransactionIsolator isolator, String tableName, String columnName) {
44+
return execute( isolator,
45+
"select " + columnName + " from " + tableName,
46+
"Could not fetch the current table value from the database" );
6047
}
6148

6249
static long getCurrentTableValue(
63-
Connection connection, String tableName, String columnName,
50+
DdlTransactionIsolator isolator,
51+
String tableName, String columnName,
6452
String segmentColumnName, String segmentValue) {
65-
final String selectCurrent =
53+
return execute( isolator,
6654
"select " + columnName + " from " + tableName
67-
+ " where " + segmentColumnName + " = '" + segmentValue + "'";
68-
try ( var select = connection.prepareStatement( selectCurrent ) ) {
69-
return resultValue( select );
70-
}
71-
catch (SQLException e) {
72-
throw new HibernateException( "Could not fetch the current table value from the database", e );
73-
}
55+
+ " where " + segmentColumnName + " = '" + segmentValue + "'",
56+
"Could not fetch the current table value from the database" );
7457
}
7558
}

hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStructure.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ public void initialize(SqlStringGenerationContext context) {
170170

171171
@Override
172172
public void registerExtraExportables(Table table, Optimizer optimizer) {
173-
table.addResyncCommand( (context, connection) -> {
174-
final String sequenceName = context.format( physicalSequenceName );
175-
final String tableName = context.format( table.getQualifiedTableName() );
173+
table.addResyncCommand( (sqlContext, isolator) -> {
174+
final String sequenceName = sqlContext.format( physicalSequenceName );
175+
final String tableName = sqlContext.format( table.getQualifiedTableName() );
176176
final String primaryKeyColumnName = table.getPrimaryKey().getColumn( 0 ).getName();
177177
final int adjustment = optimizer.getAdjustment();
178-
final long max = getMaxPrimaryKey( connection, primaryKeyColumnName, tableName );
179-
final long current = getNextSequenceValue( connection, sequenceName, context.getDialect() );
178+
final long max = getMaxPrimaryKey( isolator, primaryKeyColumnName, tableName );
179+
final long current = getNextSequenceValue( isolator, sequenceName);
180180
final long startWith = Math.max( max + adjustment, current );
181-
return new InitCommand( context.getDialect().getSequenceSupport()
181+
return new InitCommand( sqlContext.getDialect().getSequenceSupport()
182182
.getRestartSequenceString( sequenceName, startWith ) );
183183
} );
184184
}

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.hibernate.mapping.Column;
4040
import org.hibernate.mapping.PrimaryKey;
4141
import org.hibernate.mapping.Table;
42+
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
4243
import org.hibernate.service.ServiceRegistry;
4344
import org.hibernate.sql.SimpleSelect;
4445
import org.hibernate.type.StandardBasicTypes;
@@ -606,8 +607,7 @@ private void retrieveCurrentValue(
606607
final long initializationValue = storeLastUsedValue ? initialValue - 1 : initialValue;
607608
value.initialize( initializationValue );
608609
TABLE_GENERATOR_LOGGER.insertingInitialValueForSegment( value, segmentValue );
609-
try ( PreparedStatement statement = prepareStatement( connection, insertQuery, logger, listener,
610-
session ) ) {
610+
try ( var statement = prepareStatement( connection, insertQuery, logger, listener, session ) ) {
611611
statement.setString( 1, segmentValue );
612612
value.bind( statement, 2 );
613613
executeUpdate( statement, listener, insertQuery, session );
@@ -705,14 +705,14 @@ public void registerExportables(Database database) {
705705
table.addResyncCommand( this::generateResyncCommand );
706706
}
707707

708-
private InitCommand generateResyncCommand(SqlStringGenerationContext context, Connection connection) {
708+
private InitCommand generateResyncCommand(SqlStringGenerationContext context, DdlTransactionIsolator isolator) {
709709
final String sequenceTableName = context.format( physicalTableName );
710710
final String tableName = context.format( table.getQualifiedTableName() );
711711
final String primaryKeyColumnName = table.getPrimaryKey().getColumn( 0 ).getName();
712712
final int adjustment = optimizer.getAdjustment() - 1;
713-
final long max = getMaxPrimaryKey( connection, primaryKeyColumnName, tableName );
713+
final long max = getMaxPrimaryKey( isolator, primaryKeyColumnName, tableName );
714714
final long current =
715-
getCurrentTableValue( connection, sequenceTableName, valueColumnName,
715+
getCurrentTableValue( isolator, sequenceTableName, valueColumnName,
716716
segmentColumnName, segmentValue );
717717
if ( max + adjustment > current ) {
718718
final String update =

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableStructure.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,13 @@ public void initialize(SqlStringGenerationContext context) {
338338

339339
@Override
340340
public void registerExtraExportables(Table table, Optimizer optimizer) {
341-
table.addResyncCommand( (context, connection) -> {
341+
table.addResyncCommand( (context, isolator) -> {
342342
final String sequenceTableName = context.format( physicalTableName );
343343
final String tableName = context.format( table.getQualifiedTableName() );
344344
final String primaryKeyColumnName = table.getPrimaryKey().getColumn( 0 ).getName();
345345
final int adjustment = optimizer.getAdjustment();
346-
final long max = getMaxPrimaryKey( connection, primaryKeyColumnName, tableName );
347-
final long current = getCurrentTableValue( connection, sequenceTableName, valueColumnNameText );
346+
final long max = getMaxPrimaryKey( isolator, primaryKeyColumnName, tableName );
347+
final long current = getCurrentTableValue( isolator, sequenceTableName, valueColumnNameText );
348348
if ( max + adjustment > current ) {
349349
final String update =
350350
"update " + sequenceTableName

hibernate-core/src/main/java/org/hibernate/mapping/Table.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package org.hibernate.mapping;
66

77
import java.io.Serializable;
8-
import java.sql.Connection;
98
import java.util.ArrayList;
109
import java.util.Arrays;
1110
import java.util.Collection;
@@ -30,6 +29,7 @@
3029
import org.hibernate.boot.spi.MetadataBuildingContext;
3130
import org.hibernate.dialect.Dialect;
3231

32+
import org.hibernate.resource.transaction.spi.DdlTransactionIsolator;
3333
import org.jboss.logging.Logger;
3434

3535
import static java.util.Collections.emptyList;
@@ -74,7 +74,7 @@ public class Table implements Serializable, ContributableDatabaseObject {
7474
private String options;
7575

7676
private List<Function<SqlStringGenerationContext, InitCommand>> initCommandProducers;
77-
private List<BiFunction<SqlStringGenerationContext, Connection, InitCommand>> resyncCommandProducers;
77+
private List<BiFunction<SqlStringGenerationContext, DdlTransactionIsolator, InitCommand>> resyncCommandProducers;
7878

7979
@Deprecated(since="6.2", forRemoval = true)
8080
public Table() {
@@ -815,21 +815,22 @@ public List<InitCommand> getInitCommands(SqlStringGenerationContext context) {
815815
? emptyList()
816816
: initCommandProducers.stream()
817817
.map( producer -> producer.apply( context ) )
818+
.distinct()
818819
.toList();
819820
}
820821

821-
public void addResyncCommand(BiFunction<SqlStringGenerationContext, Connection, InitCommand> commandProducer) {
822+
public void addResyncCommand(BiFunction<SqlStringGenerationContext, DdlTransactionIsolator, InitCommand> commandProducer) {
822823
if ( resyncCommandProducers == null ) {
823824
resyncCommandProducers = new ArrayList<>();
824825
}
825826
resyncCommandProducers.add( commandProducer );
826827
}
827828

828-
public List<InitCommand> getResyncCommands(SqlStringGenerationContext context, Connection connection) {
829+
public List<InitCommand> getResyncCommands(SqlStringGenerationContext context, DdlTransactionIsolator isolator) {
829830
return resyncCommandProducers == null
830831
? emptyList()
831832
: resyncCommandProducers.stream()
832-
.map( producer -> producer.apply( context, connection ) )
833+
.map( producer -> producer.apply( context, isolator ) )
833834
.distinct()
834835
.toList();
835836

hibernate-core/src/main/java/org/hibernate/resource/transaction/backend/jdbc/internal/DdlTransactionIsolatorNonJtaImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public Connection getIsolatedConnection() {
4141
public Connection getIsolatedConnection(boolean autocommit) {
4242
if ( jdbcConnection == null ) {
4343
try {
44-
this.jdbcConnection = jdbcContext.getJdbcConnectionAccess().obtainConnection();
45-
44+
jdbcConnection = jdbcContext.getJdbcConnectionAccess().obtainConnection();
4645
try {
4746
if ( jdbcConnection.getAutoCommit() != autocommit ) {
4847
try {

hibernate-core/src/main/java/org/hibernate/tool/schema/internal/GeneratorSynchronizerImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ private void syncFromMetadata(
9898
Formatter formatter,
9999
GenerationTarget... targets) {
100100
try ( var ddlTransactionIsolator = tool.getDdlTransactionIsolator( jdbcContext ) ) {
101-
final var connection = ddlTransactionIsolator.getIsolatedConnection();
102101
final var context = createSqlStringGenerationContext( options, metadata );
103102
for ( var namespace : metadata.getDatabase().getNamespaces() ) {
104103
if ( schemaFilter.includeNamespace( namespace ) ) {
105104
for ( var table : namespace.getTables() ) {
106-
for ( var command : table.getResyncCommands( context, connection ) ) {
105+
for ( var command : table.getResyncCommands( context, ddlTransactionIsolator ) ) {
107106
applySqlStrings( command.initCommands(), formatter, options, targets );
108107
}
109108
}

0 commit comments

Comments
 (0)