Skip to content
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

sql: Hibernate transaction failures expects OptimisticLockException #26809

Closed
BramGruneir opened this issue Jun 18, 2018 · 3 comments
Closed
Labels
A-tools-hibernate Issues that pertain to Hibernate integration. C-investigation Further steps needed to qualify. C-label will change.

Comments

@BramGruneir
Copy link
Member

From the test
org.hibernate.test.annotations.entity.BasicHibernateAnnotationsTest testVersioning

I'm pretty sure we're doing the right thing, but I think the returned error does match what's expected.

public void testVersioning() throws Exception {
		Forest forest = new Forest();
		forest.setName( "Fontainebleau" );
		forest.setLength( 33 );
		Session s;
		Transaction tx;
		s = openSession();
		tx = s.beginTransaction();
		s.persist( forest );
		tx.commit();
		s.close();

		Session parallelSession = openSession();
		Transaction parallelTx = parallelSession.beginTransaction();
		s = openSession();
		tx = s.beginTransaction();

		forest = (Forest) parallelSession.get( Forest.class, forest.getId() );
		Forest reloadedForest = (Forest) s.get( Forest.class, forest.getId() );
		reloadedForest.setLength( 11 );
		assertNotSame( forest, reloadedForest );
		tx.commit();
		s.close();

		forest.setLength( 22 );
		try {
			parallelTx.commit();
			fail( "All optimistic locking should have make it fail" );
		}
		catch (OptimisticLockException e) {
			if ( parallelTx != null ) parallelTx.rollback();
		}
		finally {
			parallelSession.close();
		}

		s = openSession();
		tx = s.beginTransaction();
		s.delete( s.get( Forest.class, forest.getId() ) );
		tx.commit();
		s.close();
	}

Results in:

org.hibernate.TransactionException: Unable to commit against JDBC Connection
	at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:87)
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:272)
	at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:98)
	at org.hibernate.test.annotations.entity.BasicHibernateAnnotationsTest.testVersioning(BasicHibernateAnnotationsTest.java:119)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.hibernate.testing.junit4.ExtendedFrameworkMethod.invokeExplosively(ExtendedFrameworkMethod.java:45)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: org.postgresql.util.PSQLException: ERROR: restart transaction: HandledRetryableTxnError: TransactionRetryError: retry txn (RETRY_WRITE_TOO_OLD): "sql txn" id=7cba161f key=/Table/13853/1/4/0 rw=true pri=0.01714407 iso=SERIALIZABLE stat=PENDING epo=0 ts=1528920931.749022731,1 orig=1528920931.746959153,0 max=1528920931.746959153,0 wto=true rop=false seq=7
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
	at org.postgresql.jdbc.PgConnection.executeTransactionCommand(PgConnection.java:740)
	at org.postgresql.jdbc.PgConnection.commit(PgConnection.java:761)
	at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:81)
	... 18 more

The failure occurs on parallelTx.commit();, which leads me to conclude that our transaction failure doesn't equal an OptimisticLockException.

@BramGruneir BramGruneir added C-investigation Further steps needed to qualify. C-label will change. A-tools-hibernate Issues that pertain to Hibernate integration. labels Jun 18, 2018
@BramGruneir BramGruneir changed the title sql: Hiberante transaction strangeness sql: Hiberante transaction failures expects OptimisticLockException Jun 18, 2018
@BramGruneir
Copy link
Member Author

I think org.hibernate.test.annotations.xml.hbm.HbmWithIdentityTest testManyToOneAndInterface fit this as well. We return a transactionException and hibernate expects an OptimisticLockException.

expected:<class javax.persistence.OptimisticLockException> but was:<class org.hibernate.TransactionException>
	try {
			doInHibernate( this::sessionFactory, session -> {
				List<Person> persons = session.createQuery( "select p from Person p").getResultList();

				for ( int i = 0; i < persons.size(); i++ ) {
					Person person = persons.get( i );
					person.name += " Person";

					if ( i == 1 ) {
						try {
							executorService.submit( () -> {
								doInHibernate( this::sessionFactory, _session -> {
									Person _person = _session.find( Person.class, person.id );
									_person.name += " Person is the new Boss!";
								} );
							} ).get();
						}
						catch (InterruptedException|ExecutionException e) {
							fail(e.getMessage());
						}
					}
				}
			} );
		}
		catch (Exception expected) {
			assertEquals( OptimisticLockException.class, expected.getClass());
		}

@knz
Copy link
Contributor

knz commented Jun 19, 2018

Changing our pg error code in this case to match pg seems ok to me, even the concept of "optimistic lock exception" is somewhat appropriate to what our txn handling is doing.

Do you know which pg error code is used?

@jordanlewis jordanlewis changed the title sql: Hiberante transaction failures expects OptimisticLockException sql: Hibernate transaction failures expects OptimisticLockException Oct 9, 2019
@rafiss
Copy link
Collaborator

rafiss commented Jul 10, 2020

I'm closing this because this test is now getting skipped for CockroachDB, since it implicitly makes use of the Postgres BLOB pattern, which CockroachDB doesn't support.

@rafiss rafiss closed this as completed Jul 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tools-hibernate Issues that pertain to Hibernate integration. C-investigation Further steps needed to qualify. C-label will change.
Projects
None yet
Development

No branches or pull requests

3 participants