1

我在非 Web 应用程序中使用 spring,我正在使用 hibernate 来处理 DB。我遇到的问题是“registerShutdownHook();” 确实关闭了 spring 上下文容器,它没有正确关闭和关闭 JPA 的资源,因此我与 DB 的连接被最大化。

<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"/>

我使用上面介绍的配置来启动 JPA 层并使用“@Transactional”注释将 EM 注入 DAO。

也许有人可以帮助我解决我错过了什么或者我应该如何在独立环境中正确关闭 JPA 会话?

谢谢,

PS我得到的异常是:java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):连接

4

2 回答 2

0

这是使用 JPA 的应用程序 Context.xml 的另一个示例。这对我来说可以。

       <context:property-placeholder location=”classpath:jdbc.properties”/>

      <!– Connection Pool –&gt;

      <bean id=”dataSource” destroy-method=”close”&gt;

        <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 –&gt;

      <bean id=”entityManagerFactory”

                  p:dataSource-ref=”dataSource”&gt;

            <property name=”jpaVendorAdapter”&gt;

                  <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) –&gt;
      <bean id=”transactionManager”

                  p:entityManagerFactory-ref=”entityManagerFactory”/>



      <!– Activates various annotations to be detected in bean classes for eg @Autowired–&gt;

      <context:annotation-config/>



        <!– enable the configuration of transactional behavior based on annotations  –&gt;

        <tx:annotation-driven transaction-manager=”transactionManager”/>



      <!– Property Configurator –&gt;

    <bean id=”propertyConfigurer”&gt;

            <property name=”location” value=”jdbc.properties”/>

      </bean>

    <context:component-scan base-package=”com.test.dao”/>

</beans>
于 2012-10-18T13:14:49.620 回答
0

1.创建事务管理器如下:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >  
    <property name="persistenceUnitName" value="persistanceUnit"/>  
    <property name="dataSource" ref="dataSource"/>  
    <property name="persistenceXmlLocation" value="classpath:persistence.xml"/> 
    <property name="jpaVendorAdapter">  
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${db.orm.showsql}" />              
            <property name="generateDdl" value="${db.orm.generateDdl}" />               
            <property name="database" value="${db.type}"/>  
            <property name="databasePlatform" value="${db.orm.dialect}" />
        </bean>
    </property>  
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

2.使用persistance.xml(应该在类路径中)

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" 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">
  <persistence-unit name="persistanceUnit" transaction-type="RESOURCE_LOCAL">
    <description>Oracle db Persistence Unit</description>   
    <class>com.company.YourModelClass</class>
    <properties/>
  </persistence-unit>
</persistence>

3.在applicationContext.xml中添加如下注解

<context:component-scan base-package="com.yourcompany.basepackage" />

4.annoatate你的Entitymanager服务类如:

 @PersistenceContext
 private EntityManager em = null;

5.注入 TrasnsactionManager 到:

private PlatformTransactionManager platformTransactionManager = null;

6.persist对象,如:

platformTransactionManager .persist(obj);
于 2012-10-17T12:37:09.673 回答