Hibernate version:2.1.6
Mapping documents: <class name="HibernateThresholdPropertyValue"> <cache usage="read-write"/> <id type="int" name="id"> <generator class="native"/> </id> <property name="value" not-null="true" unique="true"/> <set name="thresholds" inverse="true" lazy="true"> <key column="threshold_pv_id"/> <one-to-many class="HibernateThreshold"/> </set> </class>
public class HibernateThresholdPropertyValueDao extends HibernateDaoSupport implements ThresholdPropertyValueDao { private static class GetThresholdPropertyValueCallback implements HibernateCallback { private String value; public GetThresholdPropertyValueCallback(String value) { this.value = value; } public Object doInHibernate(Session session) throws HibernateException, SQLException { Query q = session.getNamedQuery(FIND_THRESHOLD_PROPERTY_VALUE_QUERY); q.setCacheable(true); q.setText("value", value); return q.uniqueResult(); } } private static class GetOrCreatePropertyValueDaoCallback implements HibernateCallback { private String value; public GetOrCreatePropertyValueDaoCallback(String value) { this.value = value; } public Object doInHibernate(Session session) throws HibernateException, SQLException { HibernateCallback callback = new GetThresholdPropertyValueCallback(value); ThresholdPropertyValue thresholdPropertyValue = (ThresholdPropertyValue)callback.doInHibernate(session); if (thresholdPropertyValue == null) { thresholdPropertyValue = new HibernateThresholdPropertyValue(value); session.save(thresholdPropertyValue); } return thresholdPropertyValue; } }
public ThresholdPropertyValue getOrCreateThresholdPropertyValue(String value) { return (ThresholdPropertyValue)getHibernateTemplate().execute(new GetOrCreatePropertyValueDaoCallback(value)); } }
Full stack trace of any exception that occurs: 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.util.JDBCExceptionReporter] - <SQL Exception> java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of UNIQUE KEY constraint 'UQ__HibernateThresho__20C1E124'. Cannot insert duplicate key in object 'HibernateThresholdPropertyValue'. at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source) at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source) at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source) at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source) at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:256) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:508) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432) at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932) at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857) at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775) at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738) at com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao$GetOrCreatePropertyValueDaoCallback.doInHibernate(HibernateThresholdPropertyValueDao.java:62) at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:176) at com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao.getOrCreateThresholdPropertyValue(HibernateThresholdPropertyValueDao.java:74) at com.westglobal.mscape.modules.threshold.ThresholdRuntimeModule.getOrCreateThresholdPropertyValue(ThresholdRuntimeModule.java:126) at com.westglobal.mscape.modules.threshold.ThresholdRuntimeModule.processEvent(ThresholdRuntimeModule.java:101) at com.westglobal.mscape.runtime.impl.PipelineImpl.processEvent(PipelineImpl.java:30) at com.westglobal.mscape.runtime.RuntimeFrameworkImpl.processEvent(RuntimeFrameworkImpl.java:30) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:324) at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:61) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:116) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:152) at $Proxy1.processEvent(Unknown Source) at com.westglobal.mscape.agent.eventgenerator.EventGeneratorImpl.generateEvents(EventGeneratorImpl.java:44) at com.westglobal.mscape.agent.eventgenerator.CompositeEventGenerator.generateEvents(CompositeEventGenerator.java:28) at com.westglobal.mscape.agent.eventgenerator.EventGeneratorRunnable.run(EventGeneratorRunnable.java:27) at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:SqlServer 2000 and HSQLDB 1.7.2
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: 2004-10-27 16:30:41,377 INFO [Thread-15 org.springframework.jdbc.datasource.DataSourceTransactionObject] - <JDBC 3.0 Savepoint class is available> 2004-10-27 16:30:41,377 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <opened session> 2004-10-27 16:30:41,397 DEBUG [Thread-15 net.sf.hibernate.transaction.JDBCTransaction] - <begin> 2004-10-27 16:30:41,397 DEBUG [Thread-15 net.sf.hibernate.transaction.JDBCTransaction] - <current autocommit status:true> 2004-10-27 16:30:41,397 DEBUG [Thread-15 net.sf.hibernate.transaction.JDBCTransaction] - <disabling autocommit> 2004-10-27 16:30:41,397 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Bound value [org.springframework.jdbc.datasource.ConnectionHolder@1efb003] for key [org.apache.commons.dbcp.BasicDataSource@1bef1ac] to thread [Thread-15]> 2004-10-27 16:30:41,397 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Bound value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] to thread [Thread-15]> 2004-10-27 16:30:41,397 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Initializing transaction synchronization> 2004-10-27 16:30:41,407 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] bound to thread [Thread-15]> 2004-10-27 16:30:41,407 DEBUG [Thread-15 com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao] - <START GetOrCreatePropertyValueDaoCallback> 2004-10-27 16:30:41,417 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] bound to thread [Thread-15]> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <find: from HibernateThresholdPropertyValue tpv where tpv.value=:value > 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.engine.QueryParameters] - <named parameters: {value=placeOrder}> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.hql.QueryTranslator] - <compiling query> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <flushing session> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushing entities and processing referenced collections> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Processing unreferenced collections> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Scheduling collection removes/(re)creates/updates> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Dont need to execute flush> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.hql.QueryTranslator] - <HQL: from com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue tpv where tpv.value=:value > 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.hql.QueryTranslator] - <SQL: select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? )> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <checking cached query results in region: net.sf.hibernate.cache.StandardQueryCache> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.ehcache.hibernate.Plugin] - <key: sql: select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? ); parameters: ; named parameters: {value=placeOrder}> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.ehcache.hibernate.Plugin] - <Element for sql: select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? ); parameters: ; named parameters: {value=placeOrder} is null> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <query results were not found in cache> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <about to open: 0 open PreparedStatements, 0 open ResultSets> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.SQL] - <select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? )> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <preparing statement> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.type.TextType] - <binding 'placeOrder' to parameter: 1> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.loader.Loader] - <processing result set> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.loader.Loader] - <done processing result set (0 rows)> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <done closing: 0 open PreparedStatements, 0 open ResultSets> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <closing statement> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.loader.Loader] - <total objects hydrated: 0> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <initializing non-lazy collections> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <caching query results in region: net.sf.hibernate.cache.StandardQueryCache> 2004-10-27 16:30:41,417 DEBUG [Thread-15 com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao] - <ThresholdPropertyValue does not exist saving value [placeOrder]> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <saving [com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue#<null>]> 2004-10-27 16:30:41,417 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <executing insertions> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.persister.EntityPersister] - <Inserting entity: com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue (native id)> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <about to open: 0 open PreparedStatements, 0 open ResultSets> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.SQL] - <insert into HibernateThresholdPropertyValue (value) values (?) select scope_identity()> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <preparing statement> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.persister.EntityPersister] - <Dehydrating entity: [com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue#<null>]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.type.StringType] - <binding 'placeOrder' to parameter: 1> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.persister.AbstractEntityPersister] - <Natively generated identity: 5> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <done closing: 0 open PreparedStatements, 0 open ResultSets> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <closing statement> 2004-10-27 16:30:41,427 DEBUG [Thread-15 com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao] - <END GetOrCreatePropertyValueDaoCallback> 2004-10-27 16:30:41,427 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] bound to thread [Thread-15]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] bound to thread [Thread-15]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao] - <START GetOrCreatePropertyValueDaoCallback> 2004-10-27 16:30:41,427 DEBUG [Thread-15 org.springframework.transaction.support.TransactionSynchronizationManager] - <Retrieved value [org.springframework.orm.hibernate.SessionHolder@1e940a6] for key [net.sf.hibernate.impl.SessionFactoryImpl@10cc730] bound to thread [Thread-15]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <find: from HibernateThresholdPropertyValue tpv where tpv.value=:value > 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.engine.QueryParameters] - <named parameters: {value=placeOrder}> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <flushing session> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushing entities and processing referenced collections> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Processing unreferenced collections> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Scheduling collection removes/(re)creates/updates> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.Printer] - <listing entities:> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.Printer] - <com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue{value=placeOrder, thresholds=null, id=5}> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <Dont need to execute flush> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.hql.QueryTranslator] - <HQL: from com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue tpv where tpv.value=:value > 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.hql.QueryTranslator] - <SQL: select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? )> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <checking cached query results in region: net.sf.hibernate.cache.StandardQueryCache> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.ehcache.hibernate.Plugin] - <key: sql: select hibernatet0_.id as id, hibernatet0_.value as value from HibernateThresholdPropertyValue hibernatet0_ where (hibernatet0_.value=? ); parameters: ; named parameters: {value=placeOrder}> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <Checking query spaces for up-to-dateness [[HibernateThresholdPropertyValue]]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.ehcache.hibernate.Plugin] - <key: HibernateThresholdPropertyValue> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.ehcache.hibernate.Plugin] - <Element for HibernateThresholdPropertyValue is null> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.cache.StandardQueryCache] - <returning cached query results> 2004-10-27 16:30:41,427 DEBUG [Thread-15 com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao] - <ThresholdPropertyValue does not exist saving value [placeOrder]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <saving [com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue#<null>]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.SessionImpl] - <executing insertions> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.persister.EntityPersister] - <Inserting entity: com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue (native id)> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <about to open: 0 open PreparedStatements, 0 open ResultSets> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.SQL] - <insert into HibernateThresholdPropertyValue (value) values (?) select scope_identity()> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.impl.BatcherImpl] - <preparing statement> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.persister.EntityPersister] - <Dehydrating entity: [com.westglobal.mscape.modules.threshold.dao.hibernate.HibernateThresholdPropertyValue#<null>]> 2004-10-27 16:30:41,427 DEBUG [Thread-15 net.sf.hibernate.type.StringType] - <binding 'placeOrder' to parameter: 1>
Hi,
I am using Hibernate 2.1.6 and Spring 1.1. with Spring Transaction Demarcation. Following are my application context configurations
Application context configuration
==================================
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="resourceRef"><value>true</value></property>
<property name="jndiName"><value>jdbc/EventStore</value></property>
</bean>
<bean id="HibernateSessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="schemaUpdate">
<value>true</value>
</property>
<property name="mappingResources">
<list>
<value>com/package/modules/minimax/hibernate/minimax.hbm.xml</value>
<value>com/package/modules/threshold/dao/hibernate/threshold.hbm.xml</value>
<value>com/package/modules/action/javamail/dao/hibernate/mailAddress.hbm.xml</value>
<value>com/package/modules/action/persistence/action.hbm.xml</value>
<value>com/package/modules/decisiontree/persistence/hibernate/decisiontree.hbm.xml</value>
<value>com/package/modules/mapping/lookup/hibernate/mapping.hbm.xml</value>
<value>com/package/persistence/hibernate/event.hbm.xml</value>
<value>com/package/persistence/hibernate/pipeline.hbm.xml</value>
<value>com/package/persistence/hibernate/module.hbm.xml</value>
<value>com/package/reporting/dao/hibernate/timeframe.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="dataSource"><ref bean="dataSource"/></property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
<property name="sessionFactory"><ref local="HibernateSessionFactory"/></property>
</bean>
<bean id="thresholdPropertyValueDao" class="com.package.modules.threshold.dao.hibernate.HibernateThresholdPropertyValueDao">
<property name="sessionFactory"><ref bean="HibernateSessionFactory"/></property>
</bean>
<bean id="RuntimeFrameworkTarget" class="com.package.runtime.RuntimeFrameworkImpl">
<property name="pipelineStore"><ref bean="PipelineStore"/></property>
</bean>
<bean id="RuntimeFramework" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="RuntimeFrameworkTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="processEvent">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
Hibernate mapping config for the Domain Object where the problem arises
===============================================
<...>
<class name="HibernateThresholdPropertyValue">
<cache usage="read-write"/>
<id type="int" name="id">
<generator class="native"/>
</id>
<property name="value" not-null="true" unique="true"/>
<set name="thresholds" inverse="true" lazy="true">
<key column="threshold_pv_id"/>
<one-to-many class="HibernateThreshold"/>
</set>
</class>
<...>
DAO Code
=======
public class HibernateThresholdPropertyValueDao extends HibernateDaoSupport implements ThresholdPropertyValueDao {
private static class GetThresholdPropertyValueCallback implements HibernateCallback {
private String value;
public GetThresholdPropertyValueCallback(String value) {
this.value = value;
}
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.getNamedQuery(FIND_THRESHOLD_PROPERTY_VALUE_QUERY);
q.setCacheable(true);
q.setText("value", value);
return q.uniqueResult();
}
}
private static class GetOrCreatePropertyValueDaoCallback implements HibernateCallback {
private String value;
public GetOrCreatePropertyValueDaoCallback(String value) {
this.value = value;
}
public Object doInHibernate(Session session) throws HibernateException, SQLException {
HibernateCallback callback = new GetThresholdPropertyValueCallback(value);
ThresholdPropertyValue thresholdPropertyValue = (ThresholdPropertyValue)callback.doInHibernate(session);
if (thresholdPropertyValue == null) {
thresholdPropertyValue = new HibernateThresholdPropertyValue(value);
session.save(thresholdPropertyValue);
}
return thresholdPropertyValue;
}
}
public ThresholdPropertyValue getOrCreateThresholdPropertyValue(String value) {
return (ThresholdPropertyValue)getHibernateTemplate().execute(new GetOrCreatePropertyValueDaoCallback(value));
}
}
The TransactionProxyFactoryBean target calls processEvent which eventually calls the HibernateThresholdPropertyValueDao method getOrCreateThresholdPropertyValue(...).
The HibernateThresholdPropertyValueDao method getOrCreateThresholdPropertyValue(...) is called a few times from the processEvent
The second time the method getOrCreateThresholdPropertyValue(...) is called I get the following error.
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of UNIQUE KEY constraint 'UQ__HibernateThresho__2F10007B'. Cannot insert duplicate key in object 'HibernateThresholdPropertyValue'.
2004-10-27 13:29:10,712 DEBUG [Thread-15 net.sf.hibernate.util.JDBCExceptionReporter] - <SQL Exception>
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Violation of UNIQUE KEY constraint 'UQ__HibernateThresho__2F10007B'. Cannot insert duplicate key in object 'HibernateThresholdPropertyValue'.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.execute(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:256)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:508)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:432)
at net.sf.hibernate.impl.ScheduledIdentityInsertion.execute(ScheduledIdentityInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:932)
at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
I am using the ehcache plugin with default configurations.
It seems that the query cache is unaware that it needs to be updated on the second hit during the transaction. The first call to
getOrCreateThresholdPropertyValue makes a call to session.save(thresholdPropertyValue).
This problem only manifested when the transaction manager was introduced. If the transaction manager is removed the
query cache is aware that it needs to be updated (on the second call to getOrCreateThresholdPropertyValue(...)).
It works of course if I set the query's forceCacheToRefresh to true.
Is this what I have to do to synchronise the queryCache during a transaction?
Any help would be greatly appreciated.
Sinead
|