我正在使用Spring 3.1.1.RELEASE,Hibernate 4.1.0.Final,JPA 2.0和JBoss 7.1.1.Final构建Web应用程序(WAR)。我想懒惰地在我的控制器中加载一些对象(在它提供视图之前),所以我认为我会给“OpenEntityManagerInViewInterceptor”一个旋转。我在我的SPring应用程序上下文文件中配置了这个...
<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>
我想让这些实体懒得加载......
@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;
我的控制器试图非常简单地访问这些字段......
@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>
答案 0 :(得分:0)
使用JTA + Hibernate时的连接释放模式默认设置为“after_statment”。我想这就是你得到所有这些日志的原因。
我建议您通过在hibernate配置中指定以下参数将此默认值更改为“on_close”或“after_transaction”:
hibernate.connection.release_mode="on_close"
hibernate.connection.release_mode
的所有可能值均为after_transaction
,after_statement
和on_close