-
Notifications
You must be signed in to change notification settings - Fork 12
Convert SQLException's to Hibernate ORM exception's. #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
4b65eb2
Introduce batch support.
vbabanin 5b17d58
Add tests and refactor MongoStatements.
vbabanin 3f1ebcb
Merge branch 'main' into HIBERNATE-40
vbabanin ded2bb7
Rename tests.
vbabanin 92d9c41
Rename variables.
vbabanin b78cb77
Rename methods, add tests.
vbabanin a7f7792
Revert to private.
vbabanin 7372d56
Add JDBC exception handling.
vbabanin 330de0f
Apply spotless.
vbabanin 5c81496
Apply spotless.
vbabanin e4d7afd
Revert SqlTransientException handling.
vbabanin 183aafc
Remove SQlConsumer.
vbabanin 9128c9e
Convert SQLException's to Hibernate ORM exception's.
vbabanin 35dbc91
Remove public modifier.
vbabanin 6fe48b4
Remove disabled tests.
vbabanin 9577603
Fix issues.
vbabanin 76acbc5
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 39df511
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 11a2b07
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 510bc2d
Update src/integrationTest/java/com/mongodb/hibernate/jdbc/MongoPrepa…
vbabanin 6faa9cb
Fix batch update count reporting.
vbabanin b27ef0e
Merge branch 'main' into HIBERNATE-40
vbabanin 7c8a702
Make NULL_SQL_STATE private.
vbabanin 51103be
Remove final in parameters.
vbabanin 06b1ee0
Merge branch 'HIBERNATE-40' into HIBERNATE-95
vbabanin bbade89
Move failpoint to beforeEach and afterEach.
vbabanin 0cda4bc
Add afterAll.
vbabanin ad94e8f
Update src/integrationTest/java/com/mongodb/hibernate/junit/MongoExte…
vbabanin 17af7e8
Use JdbcException for all cases.
vbabanin c7a67dc
Update src/integrationTest/java/com/mongodb/hibernate/exception/Excep…
vbabanin 9d0b258
Move tests to .*hibernate root package.
vbabanin 2057907
Merge branch 'main' into HIBERNATE-95
vbabanin 1a22030
Spotless apply.
vbabanin 8414f45
Merge branch 'main' into HIBERNATE-95
vbabanin 95d3448
Spotless apply.
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...onTest/java/com/mongodb/hibernate/exception/AbstractExceptionHandlingIntegrationTest.java
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright 2025-present MongoDB, Inc. | ||
| * | ||
| * 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 com.mongodb.hibernate.exception; | ||
|
|
||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.hibernate.junit.InjectMongoClient; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.BsonString; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
| import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; | ||
| import org.junit.jupiter.api.AfterEach; | ||
|
|
||
| class AbstractExceptionHandlingIntegrationTest implements SessionFactoryScopeAware { | ||
| private static final String FAIL_COMMAND_NAME = "failCommand"; | ||
|
|
||
| @InjectMongoClient | ||
| private static MongoClient mongoClient; | ||
|
|
||
| SessionFactoryScope sessionFactoryScope; | ||
|
|
||
| @Override | ||
| public void injectSessionFactoryScope(SessionFactoryScope sessionFactoryScope) { | ||
| this.sessionFactoryScope = sessionFactoryScope; | ||
| } | ||
|
|
||
| static void configureFailPointErrorCode(int errorCode) { | ||
| BsonDocument failPointCommand = BsonDocument.parse("{" | ||
| + " configureFailPoint: \"failCommand\"," | ||
| + " mode: { times: 1 }," | ||
| + " data: {" | ||
| + " failCommands: [\"insert\"]," | ||
| + " errorCode: " + errorCode | ||
| + " errorLabels: [\"TransientTransactionError\"]" | ||
| + " }" | ||
| + "}"); | ||
vbabanin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mongoClient.getDatabase("admin").runCommand(failPointCommand); | ||
| } | ||
|
|
||
| private static void disableFailPoint(final String failPoint) { | ||
stIncMale marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| BsonDocument failPointDocument = | ||
| new BsonDocument("configureFailPoint", new BsonString(failPoint)).append("mode", new BsonString("off")); | ||
| mongoClient.getDatabase("admin").runCommand(failPointDocument); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| disableFailPoint(FAIL_COMMAND_NAME); | ||
| } | ||
| } | ||
176 changes: 176 additions & 0 deletions
176
...ntegrationTest/java/com/mongodb/hibernate/exception/ExceptionHandlingIntegrationTest.java
This file contains hidden or 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,176 @@ | ||
| /* | ||
| * Copyright 2025-present MongoDB, Inc. | ||
| * | ||
| * 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 com.mongodb.hibernate.exception; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import com.mongodb.MongoException; | ||
| import com.mongodb.client.MongoCollection; | ||
| import com.mongodb.hibernate.junit.InjectMongoCollection; | ||
| import com.mongodb.hibernate.junit.MongoExtension; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
| import java.sql.BatchUpdateException; | ||
| import java.sql.SQLException; | ||
| import java.sql.SQLIntegrityConstraintViolationException; | ||
| import java.sql.SQLTimeoutException; | ||
| import org.bson.BsonDocument; | ||
| import org.hibernate.cfg.AvailableSettings; | ||
| import org.hibernate.exception.ConstraintViolationException; | ||
| import org.hibernate.exception.GenericJDBCException; | ||
| import org.hibernate.testing.orm.junit.DomainModel; | ||
| import org.hibernate.testing.orm.junit.ServiceRegistry; | ||
| import org.hibernate.testing.orm.junit.SessionFactory; | ||
| import org.hibernate.testing.orm.junit.Setting; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| @ExtendWith(MongoExtension.class) | ||
| @SessionFactory(exportSchema = false) | ||
| @DomainModel(annotatedClasses = {ExceptionHandlingIntegrationTest.Item.class}) | ||
| class ExceptionHandlingIntegrationTest extends AbstractExceptionHandlingIntegrationTest { | ||
| private static final String COLLECTION_NAME = "items"; | ||
|
|
||
| @InjectMongoCollection(COLLECTION_NAME) | ||
| private static MongoCollection<BsonDocument> mongoCollection; | ||
|
|
||
| @Test | ||
| void testConstraintViolationExceptionThrown() { | ||
| mongoCollection.insertOne(BsonDocument.parse("{_id: 1}")); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(ConstraintViolationException.class) | ||
| .hasMessageContaining("Failed to execute operation") | ||
| .cause() | ||
| .isInstanceOf(SQLIntegrityConstraintViolationException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(ints = {1000, 1200}) | ||
| void testGenericExceptionThrown(int errorCode) { | ||
| configureFailPointErrorCode(errorCode); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(GenericJDBCException.class) | ||
| .hasMessageContaining("Failed to execute operation") | ||
| .cause() | ||
| .isInstanceOf(SQLException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void testTimeoutExceptionThrown() { | ||
| configureFailPointErrorCode(50); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(GenericJDBCException.class) | ||
| .hasMessageContaining("Timeout while waiting for operation to complete") | ||
| .cause() | ||
| .isInstanceOf(SQLTimeoutException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
|
|
||
| @Nested | ||
| @ServiceRegistry(settings = @Setting(name = AvailableSettings.STATEMENT_BATCH_SIZE, value = "2")) | ||
| @ExtendWith(MongoExtension.class) | ||
| @SessionFactory(exportSchema = false) | ||
| @DomainModel(annotatedClasses = {ExceptionHandlingIntegrationTest.Item.class}) | ||
| class Batch extends AbstractExceptionHandlingIntegrationTest { | ||
|
|
||
| private static final String EXCEPTION_MESSAGE = "Batch execution failed"; | ||
|
|
||
| @Test | ||
| void testConstraintViolationExceptionThrown() { | ||
| mongoCollection.insertOne(BsonDocument.parse("{_id: 1}")); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| session.persist(new Item(2)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(ConstraintViolationException.class) | ||
| .hasMessageContaining(EXCEPTION_MESSAGE) | ||
| .cause() | ||
| .isInstanceOf(BatchUpdateException.class) | ||
| .cause() | ||
| .isInstanceOf(SQLIntegrityConstraintViolationException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(ints = {1000, 1200}) | ||
| void testGenericExceptionThrown(int errorCode) { | ||
| configureFailPointErrorCode(errorCode); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| session.persist(new Item(2)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(GenericJDBCException.class) | ||
| .hasMessageContaining(EXCEPTION_MESSAGE) | ||
| .cause() | ||
| .isInstanceOf(BatchUpdateException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
|
|
||
| @Test | ||
| void testTimeoutExceptionThrown() { | ||
| configureFailPointErrorCode(50); | ||
| assertThatThrownBy(() -> { | ||
| sessionFactoryScope.inTransaction(session -> { | ||
| session.persist(new Item(1)); | ||
| session.persist(new Item(2)); | ||
| }); | ||
| }) | ||
| .isInstanceOf(GenericJDBCException.class) | ||
| .hasMessageContaining("Batch execution failed]") | ||
| .cause() | ||
| .isInstanceOf(BatchUpdateException.class) | ||
| .cause() | ||
| .isInstanceOf(SQLTimeoutException.class) | ||
| .hasRootCauseInstanceOf(MongoException.class); | ||
| } | ||
| } | ||
|
|
||
| @Entity | ||
| @Table(name = COLLECTION_NAME) | ||
| static class Item { | ||
| @Id | ||
| int id; | ||
|
|
||
| Item() {} | ||
|
|
||
| Item(int id) { | ||
| this.id = id; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.