Hi,
It's hard to describe the question but I will try...
We have a detached singleton object which has 2 maps (2 value maps, using association tables, unique constraints are added to the key columns). The accesses to the maps are guarded by 2 RW locks to make sure no threads can modify each map at the same time. Also, when a map is modified, the singleton object will be written to the database to update the change. The save to database operation is synchronized using another lock.
However, once in a while we will hit the "unique key constraint violation" (see below stack trace) when we store the singleton to database (happens when a lot of threads are accessing the singleton). My guessing is since the maps and the singleton are not actually using the same lock, it's possible that when hibernate saves the singleton to the db some thread is making changes to the maps and causes it.
However, I can't prove that's the case. I tried to write a small test case to have multiple threads read/write the map at the same time but couldn't reproduce the problem.
Since I am not familiar with Hibernate internals, do you guys know in what condition that may happen? Is my guessing in the right direction? Will it be solved if we properly guard the maps and the containing obj?
Thanks a lot for your help.
--Jiunjiun
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
[b]Hibernate version:[/b]
2.1.7c
[b]Mapping documents:[/b]
<class name="MonitoringServiceJDO" table="hd_monitoring_service">
<id name="id" column="object_id" type="long" access="field">
<generator class="sequence">
<param name="sequence">seq_hd_object_id</param>
</generator>
</id>
<property name="name" column="name" type="string" access="field"/>
<map name="enableds" table="hd_monitoring_enabled_object" cascade="all" access="field">
<key column="service_object_id"/>
<index column="key" type="string"/>
<element column="value" type="boolean"/>
</map>
<map name="pollingIntervals" table="hd_monitoring_polling_interval" cascade="all" access="field">
<key column="service_object_id"/>
<index column="key" type="string"/>
<element column="value" type="long"/>
</map>
</class>
[b]Code between sessionFactory.openSession() and session.close():[/b]
N/A
[b]Full stack trace of any exception that occurs:[/b]
Mar 24, 2005 12:19:47 PM
com.sun.hss.services.opgrpmgmt.impl.OpGrpMgmtUtilityFINE: could not insert
collection rows: [com.sun.hss.domain.internal.MonitoringServiceJDO.enableds#1]
java.sql.SQLException: ERROR: Cannot insert a duplicate key into unique index
uq_hd_monitoring_enabled_object
at org.postgresql.core.QueryExecutor.executeV2(QueryExecutor.java:288)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:104)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:517)
at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
at
org.postgresql.jdbc1.AbstractJdbc1Statement.executeUpdate(AbstractJdbc1Statement.java:273)
at
com.mchange.v2.sql.filter.FilterPreparedStatement.executeUpdate(FilterPreparedStatement.java:71)
at
net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at
net.sf.hibernate.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:610)
at
net.sf.hibernate.impl.ScheduledCollectionUpdate.execute(ScheduledCollectionUpdate.java:49)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2394)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
at
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at
net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at
(other part ignored)
[b]Name and version of the database you are using:[/b]
Postgres 7.2.3
[b]The generated SQL (show_sql=true):[/b]
N/A
[b]Debug level Hibernate log excerpt:[/b]
Mar 24, 2005 12:19:47 PM net.sf.hibernate.util.JDBCExceptionReporter
logExceptions
WARNING: SQL Error: 0, SQLState: null
Mar 24, 2005 12:19:47 PM net.sf.hibernate.util.JDBCExceptionReporterd
logExceptions
SEVERE: ERROR: Cannot insert a duplicate key into unique index
uq_hd_monitoring_enabled_object