1

我正在使用 Spring 3.1.1.RELEASE、Hibernate 4.1.0.Final、JPA 2.0 和 JBoss 7.1.1.Final 构建一个 Web 应用程序 (WAR)。我想在我的控制器中延迟加载一些对象(在它提供视图之前),所以我想我会给“OpenEntityManagerInViewInterceptor”一个旋转。我在我的 SPring 应用程序上下文文件中配置了这个……</p>

<mvc:interceptors>
    …
    <mvc:interceptor>
            <mvc:mapping path="/contracts/*"/>
            <bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">  
                <property name="entityManagerFactory" ref="entityManagerFactory" />  
            </bean>
    </mvc:interceptor>
</mvc:interceptors>

我有这些我想延迟加载的实体……</p>

@Entity
@Table(name = "cb_contract")
@Cacheable
@DataTransferObject
public class Contract implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    @GeneratedValue(generator = "uuid-strategy")
    private String id;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PRODUCT_ID")
    @NotNull
    private Product product;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ORGANIZATION_ID")
    @NotNull
    private Organization org;

我的控制器尝试非常简单地访问这些字段……</p>

@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
public ModelAndView doGetUpdate(final HttpServletRequest request,
                                final Model model,
                                @PathVariable final String id)
{
    String nextPage = "landing";
    final Contract contract = m_contractService.findById(id);
    if (contract != null)
    {
        final ContractForm contractForm = new ContractForm(contract.getProduct(), 
        ...

不幸的是,当我尝试访问我的 JSP 页面时,加载大约需要 20 秒(之前只需要 2 秒)。对于页面上的每个实体,Spring 中的某些东西似乎反复“寻找要加入的 JTA 事务”和“获取 JDBC 连接”,而不仅仅是那些我想要延迟加载的实体。我可以做些什么来更好地配置它并使其更快?

谢谢, - 戴夫

编辑: 这是配置。使用 Spring 3.1,我不需要 persistence.xml 文件。这是 entityManagerFactory ...

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="org.mainco.springboard" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaPropertyMap" ref="jpaPropertyMap" />
</bean>

<util:map id="jpaPropertyMap">
    <entry key="show_sql" value="true" />
    <entry key="dialect" value="org.hibernate.dialect.MySQLDialect" />
    <entry key="hibernate.hbm2ddl.auto" value="validate" />
    <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
    <entry key="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
    <entry key="hibernate.cache.use_second_level_cache" value="true" />
    <entry key="hibernate.cache.use_query_cache" value="false" />
    <entry key="hibernate.generate_statistics" value="false" />
</util:map>

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<jee:jndi-lookup jndi-name="java:jboss/datasources/MySqlDS" id="dataSource" expected-type="javax.sql.DataSource"/>

<tx:jta-transaction-manager />

我使用 JBoss 7.1.1.Final 附带的默认事务管理器。这是数据源

        <datasources>
            <datasource jndi-name="java:jboss/datasources/MySqlDS" pool-name="MySqlDS" enabled="true" use-java-context="true">
                <connection-url>jdbc:mysql://localhost:3306/mydb_db</connection-url>
                <driver>mysql</driver>
                <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                <pool>
                    <min-pool-size>10</min-pool-size>
                    <max-pool-size>100</max-pool-size>
                    <prefill>true</prefill>
                </pool>
                <security>
                    <user-name>my_db</user-name>
                    <password>my_db</password>
                </security>
                <statement>
                    <prepared-statement-cache-size>32</prepared-statement-cache-size>
                    <share-prepared-statements>true</share-prepared-statements>
                </statement>
            </datasource>
            <drivers>
                <driver name="mysql" module="com.mysql"/>
            </drivers>
        </datasources>
4

1 回答 1

0

使用 JTA + Hibernate 时的连接释放模式默认设置为“after_statment”。我认为这就是你得到所有这些日志的原因。

我可以建议您通过在 hibernate config 中指定以下参数来将此默认值更改为“on_close”或“after_transaction”:

hibernate.connection.release_mode="on_close"

的所有可能值hibernate.connection.release_modeafter_transactionafter_statementon_close

于 2013-02-14T13:21:03.900 回答