私のアプリケーションは、休止状態のJPAを使用してpostgres DBに接続します。DBにデータを書き込むスレッドが1つしかない場合、書き込みを完了するのに永遠に時間がかかり、10スレッドを使用すると、DBでデッドロックの問題が発生します。JPAはスレッド制御を自動的に処理するべきではありませんか?
これが私の休止状態の構成です。
applicationContext.xml:
<bean id="entityManagerFactory" class="javax.persistence.Persistence"
factory-method="createEntityManagerFactory" destroy-method="close" scope="singleton">
<constructor-arg value="${persistence_unit}" />
<constructor-arg>
<util:map>
<entry key="javax.persistence.jdbc.url" value="${db_url}" />
<entry key="javax.persistence.jdbc.user" value="${db_user}" />
<entry key="javax.persistence.jdbc.password" value="${db_password}" />
<!-- Connection provider properties.-->
<entry key="hibernate.c3p0.max_size" value="${hibernate_c3p0_max_size}" />
<entry key="hibernate.c3p0.min_size" value="${hibernate_c3p0_min_size}" />
<entry key="hibernate.c3p0.acquire_increment" value="${hibernate_c3p0_acquire_increment}" />
<entry key="hibernate.c3p0.idle_test_period" value="${hibernate_c3p0_idle_test_period}" />
<entry key="hibernate.c3p0.max_statements" value="${hibernate_c3p0_max_statements}" />
<entry key="hibernate.c3p0.timeout" value="${hibernate_c3p0_timeout}" />
</util:map>
</constructor-arg>
</bean>
operation-config.properties:
hibernate_c3p0_max_size=100
hibernate_c3p0_min_size=1
hibernate_c3p0_acquire_increment=1
hibernate_c3p0_idle_test_period=60
hibernate_c3p0_max_statements=0
hibernate_c3p0_timeout=30
hibernate_max_fetch_depth=3
hibernate_generate_statistics=false
マルチスレッドの書き込みで、次のエラーが発生しました:
2013-Feb-12 14:04:26.621 ERROR o.h.u.JDBCExceptionReporter o.h.u.JDBCExceptionReporter,101 Batch entry 0 insert into TAGS (name, tagtype, id) values ('2135daa6-6d88-43aa-9a7d-cc5ee5735893', '1', '10004') was aborted. Call getNextException to see the cause.
2013-Feb-12 14:04:26.621 WARN o.h.u.JDBCExceptionReporter o.h.u.JDBCExceptionReporter,100 SQL Error: 0, SQLState: 23505
2013-Feb-12 14:04:26.622 ERROR o.h.u.JDBCExceptionReporter o.h.u.JDBCExceptionReporter,101 ERROR: duplicate key value violates unique constraint "tags_name_key"
2013-Feb-12 14:04:26.627 ERROR o.h.e.d.AbstractFlushingEventListener o.h.e.d.AbstractFlushingEventListener,324 Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into TAGS (name, tagtype, id) values ('2135daa6-6d88-43aa-9a7d-cc5ee5735893', '1', '10004') was aborted. Call getNextException to see the cause.
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@412ad7b3 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
Feb 12, 2013 2:05:13 PM com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@412ad7b3 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@86fbfc2 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@60965de1 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@2fed9051 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@159dce6c