我正在使用Spring开发一个应用程序,它使用带有@PersistenceContext
注入的实体管理器来指导数据库。应用程序在多个线程中执行,每个线程都在自己的@Transactional
范围内。
作为线程进程的一部分,它应该去调用:
entityManager.createQuery("some statement").getResultList()
如果数据库上存在对象,则应该返回,如果不存在,它将保留它。因为我使用多个线程,所以我将调用放在共享对象的同步块中,如下所示:
synchronized (getLock())
{
List<SomeObject> listOfObjects = entityManager.createQuery("some statement").getResultList();
if(listOfObjects == null || listOfObjects.size() < 1)
{
createObject();
}
}
问题在于,当我正在运行此过程时,该过程会卡在getResultList
的调用上,并且永远不会释放。而且,当我运行代码并只创建一个线程时,我会得到相同的行为。
如果我将flushMode更改为COMMIT,则该进程被释放,但结果反映不好,因为不同的线程可能在不同的时间结束,而创建该对象的线程不一定是先结束的那个。
我的持久性xml是:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="syncAdapterUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.connection.pool_size" value="10"/>
<property name="format_sql" value="false"/>
<property name="hibernate.jdbc.batch_size" value="50" />
<property name="hibernate.cache.use_query_cache" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="false" />
</properties>
</persistence-unit>
</persistence>
spring bean xml:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="managerFactoryBean" class="com.amdocs.dim.ManagerFactory" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" destroy-method="destroy">
<property name="dataSource" ref="myDataSource"/>
<property name="jpaVendorAdapter" ref="hibernateVendorAdapter"/>
<property name="persistenceUnitName" value="syncAdapterUnit"/>
</bean>
<bean id="hibernateVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="false"/>
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="database" value="ORACLE"/>
</bean>
<bean id="cifDBProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="location" value="classpath:APP-INF/conf/database.properties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_FALLBACK"/>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" scope="singleton">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
我认为刷新过程是阻止应用程序但无法理解原因的过程。