问题描述
我在非web应用程序中使用spring,并使用hibernate处理数据库。我遇到的问题是,虽然registerShutdownHook();关闭spring上下文容器它没有正确关闭并关闭JPA的资源,所以我与数据库的连接正在被刷新。
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =persistenceUnitNamevalue =pu/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =generateDdlvalue =false/>
< property name =showSqlvalue =false/>
< property name =databasePlatformvalue =org.hibernate.dialect.MySQL5Dialect/>
< / bean>
< / property>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< tx:注解驱动的事务管理器=transactionManager/>
< bean class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor/>
< bean class =org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor/>
我使用上面提到的配置来激活JPA层并使用@Transactional注释将EM注入到DAO's。
也许有人可以帮我解决我错过了什么,或者我应该如何处理在独立环境中正确关闭JPA会话?
谢谢,
PS异常我得到的是:java.net.SocketException:没有可用的缓冲区空间(最大连接达到?):连接
是使用JPA的应用程序Context.xml的另一个示例。它适用于我。
< context:property-placeholder location =classpath:jdbc.properties/>
< bean id =dataSourcedestroy-method =close>
< property name =driverClassvalue =$ {jdbc.driverClass}/>
< property name =jdbcUrlvalue =$ {jdbc.url}/>
< property name =uservalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< / bean>
< bean id =entityManagerFactory
p:dataSource-ref =dataSource>
< property name =jpaVendorAdapter>
< bean>
< property name =showSqlvalue =$ {jdbc.showSql}/>
< / bean>
< / property>
< / bean>
< bean id =transactionManager
p:entityManagerFactory-ref =entityManagerFactory/>
< context:annotation-config />
< tx:注解驱动的事务管理器=transactionManager/>
< bean id =propertyConfigurer>
< property name =locationvalue =jdbc.properties/>
< / bean>
< context:component-scan base-package =com.test.dao/>
< / beans>
I am utilizing spring in non web application and I am using hibernate for working with DB. Problem I am experiencing is that while "registerShutdownHook();" does close spring context container it does not properly shut down and close resources for JPA so my connections to DB are getting maxed out.
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="pu" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="showSql" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
I use configuration presented above to fire up JPA layer and use "@Transactional" annotations to inject EM's into DAO's.
Maybe someone could help me out what am I missing or how should I handle proper closing of JPA sessions in standalone environment ?
Thank you,
P.S. Exception I am getting is: java.net.SocketException: No buffer space available (maximum connections reached?): connect
This is another example of application Context.xml with JPA. It works fine for me.
<context:property-placeholder location="classpath:jdbc.properties"/>
<!– Connection Pool –>
<bean id="dataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!– JPA EntityManagerFactory –>
<bean id="entityManagerFactory"
p:dataSource-ref="dataSource">
<property name="jpaVendorAdapter">
<bean>
<property name="database" value="${jdbc.database}"/>
<property name="showSql" value="${jdbc.showSql}"/>
</bean>
</property>
</bean>
<!– Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) –>
<bean id="transactionManager"
p:entityManagerFactory-ref="entityManagerFactory"/>
<!– Activates various annotations to be detected in bean classes for eg @Autowired–>
<context:annotation-config/>
<!– enable the configuration of transactional behavior based on annotations –>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!– Property Configurator –>
<bean id="propertyConfigurer">
<property name="location" value="jdbc.properties"/>
</bean>
<context:component-scan base-package="com.test.dao"/>
</beans>
这篇关于Spring独立应用程序中的JPA / Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!