MySQL Parallel laufende EntityManager kommen sich in die Quere

_Andi91

Bekanntes Mitglied
Hi,

ich habe ein Problem mit Mysql in Verbindung mit Hibernate JPA sobald ich zwei EntityManager Instanzen parallel laufe lasse.
Ich weis aber nicht mehr weiter wo das Problem liegen könnte.
Ich hab schon verschiedene Sachen ausprobiert u.a. auch in der my.ini (mysql config) transaction-isolation = READ-COMMITTED gesetzt.
Habe es auch mit MySQL5InnoDBDialect statt MySQLDialect probiert.
Leider aber alles ohne Erfolg. Solange nur ein EntityManager "parallel" läuft funktioniert alles ohne Probleme.

Das komplette Projekt ist hier zu finden:
https://www.file-upload.net/download-12760219/test-jpa2.zip.html

Die mysql Datenbank ist aus einer xampp Installation und hat die Version 5.1.41.

Der Code liefert diese Ausgabe (Ich hab den Code leider gerade nicht da, den kann ich aber heute Abend noch mit reinschreiben):
Code:
Okt 12, 2017 9:10:46 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: db
    ...]
Okt 12, 2017 9:10:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Okt 12, 2017 9:10:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Okt 12, 2017 9:10:47 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Okt 12, 2017 9:10:47 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Okt 12, 2017 9:10:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Okt 12, 2017 9:10:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost/jf]
Okt 12, 2017 9:10:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=root, password=****}
Okt 12, 2017 9:10:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Okt 12, 2017 9:10:47 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Okt 12, 2017 9:10:48 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
+++ Save user; id: 2; x: 0
+++ Save user; id: 3; x: 0
--- Delete user; id: 2; x: 0
--- Delete user; id: 3; x: 0
+++ Save user; id: 2; x: 1
+++ Save user; id: 3; x: 1
--- Delete user; id: 2; x: 1
+++ Save user; id: 2; x: 2
--- Delete user; id: 2; x: 2
+++ Save user; id: 2; x: 3
--- Delete user; id: 2; x: 3
+++ Save user; id: 2; x: 4
--- Delete user; id: 2; x: 4
+++ Save user; id: 2; x: 5
--- Delete user; id: 2; x: 5
+++ Save user; id: 2; x: 6
--- Delete user; id: 2; x: 6
+++ Save user; id: 2; x: 7
--- Delete user; id: 2; x: 7
Okt 12, 2017 9:10:50 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1062, SQLState: 23000
+++ Save user; id: 2; x: 8
Okt 12, 2017 9:10:50 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Duplicate entry '2' for key 'PRIMARY'
--- Delete user; id: 2; x: 8
+++ Save user; id: 2; x: 9
--- Delete user; id: 2; x: 9
+++ Save user; id: 2; x: 10
Okt 12, 2017 9:10:50 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
INFO: HHH000010: On release of batch it still contained JDBC statements
Exception in thread "Thread-2" --- Delete user; id: 2; x: 10
+++ Save user; id: 2; x: 11
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)
    at Main.doInsertDelete(Main.java:30)
    at Main.lambda$1(Main.java:14)
    at Main$$Lambda$2/1325465767.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:59)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
--- Delete user; id: 2; x: 11    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2884)
+++ Save user; id: 2; x: 12

    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3384)
    at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:447)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:333)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:335)
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
    at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1247)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:162)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
    at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:774)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:747)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:752)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)
    ... 4 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '2' for key 'PRIMARY'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)--- Delete user; id: 2; x: 12

    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
+++ Save user; id: 2; x: 13
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
    at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
    at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
    ... 24 more
--- Delete user; id: 2; x: 13
+++ Save user; id: 2; x: 14
...

-> Es werden 2 Threads gestartet die jeweils in einer Schleife einen User speichern und anschließend wieder löschen. Thread 1 speichert/löscht den User mit der ID 2 und Thread 2 den User mit der ID 3.
Jeder Thread hat seinen eigenen EntityManager und somit auch seine eigene Transaktion.
 
Zuletzt bearbeitet von einem Moderator:

_Andi91

Bekanntes Mitglied
Thema hat sich erledigt, es lag wohl doch an der Option transaction-isolation = READ-COMMITTED.
 
Zuletzt bearbeitet:

Ähnliche Java Themen


Oben