【问题标题】:How Can I Reduce Spring Jpa Hibernate Project Deployment Time?如何减少 Spring Jpa Hibernate 项目部署时间?
【发布时间】:2017-07-18 22:51:03
【问题描述】:

我有一个 web 项目,它是开发 spring、jpa 和 hibernate 以及数据库 mysql 和服务器 Apache Tomcat 8。我的项目大小为 87 MB。我的数据库大小为 1 GB。当我运行我的项目时,部署需要 20 多分钟。我也申请了 Hikari 连接池,但问题没有解决。我提供配置文件和其他相关文件。

这是我的 database.properties 文件

################### JDBC Configuration ##########################
jdbc.driverClassName=com.mysql.jdbc.Driver

 jdbc.url=jdbc:mysql://localhost:3306
/emdemo_liza?verifyServerCertificate=false&useSSL=false&requireSSL=false
jdbc.username=root
jdbc.password=1234

################### Hibernate Configuration ##########################
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.generate_statistics=true
hibernate.connection.charSet=UTF-8
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider

################## For List insertion Hiber Config ######################
###hibernate.order_inserts=true###
####hibernate.order_updates=true####

这是我的 applicationContext-db.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org
/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org
/schema/tx/spring-tx-4.2.xsd">

<!-- Scan for property files -->
<context:property-placeholder location="classpath:META-INF/spring/*
 .properties" />

<!-- Transaction Manager -->
<bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- Detect @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" />


<!-- Entity Manager Factory -->
 <bean id="entityManagerFactory" class="org.springframework.orm.jpa
 .LocalContainerEntityManagerFactoryBean">

 <property name="dataSource" ref="dataSource" />
 <property name="jpaVendorAdapter">

        <!-- Define Hibernate JPA Vendor Adapter -->
<bean class="org.springframework.orm.jpa.vendor
.HibernateJpaVendorAdapter">
    <property name="generateDdl" value="true" />
    <property name="database" value="MYSQL" />
</bean>
</property>
    <!-- Persistence Unit -->
    <property name="persistenceUnitName" value="persistenceUnit" />
</bean>

 <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
    p:username="${jdbc.username}" p:password="${jdbc.password}" />


 <bean class="org.springframework.orm.jpa.support
  .PersistenceAnnotationBeanPostProcessor" />
 <bean id="jdbcTemplate"
   class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

这是我的实体管理器类

package com.netizenbd.dao.service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.hibernate.HibernateException;
import org.springframework.transaction.annotation.Transactional;

import com.netizenbd.dao.EntityDao;

import javassist.bytecode.SignatureAttribute.TypeVariable;

public class EntityService<E>  implements EntityDao<E> {

    @PersistenceContext(unitName="persistenceUnit")
    protected EntityManager entityManager;

    protected E instance;
    private Class<E> entityClass;

    @Transactional
    public void persist(E e) throws HibernateException{     
        getEntityManager().persist(e);
        getEntityManager();

    }
    @Transactional
    public void merge(E e) throws HibernateException{     
        getEntityManager().merge(e);
    }
    @Transactional
    public void remove(Object id) throws Exception{     
        getEntityManager().remove((E)getEntityManager().find(getEntityClass(), id));

    }

    @Transactional(readOnly = true)
    public E findById(Object id) throws Exception {     
        return (E)getEntityManager().find(getEntityClass(), id);    
    }

    @Transactional(readOnly = true)
    public E findAcademicYearObj(Object id, Object year) throws Exception {     
        return (E)getEntityManager().find(getEntityClass(), id +"and"+ year);    
    }

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<E> findAll() throws Exception{
        return getEntityManager().createQuery("Select t from " + getEntityClass().getSimpleName() + " t").getResultList();
    }
}

请任何人帮助我如何使用 tomcat 服务器在短时间内快速部署我的项目。

谢谢

【问题讨论】:

  • 我建议分析框架级语句的日志文件,即。 Spring 和 hibernate 并确定消耗时间最多的地方。 (如果INFO 日志不足,则启用DEBUG 级别日志记录)这将有助于缩小潜在原因,因为目前共享的信息充其量是没有帮助的。
  • hibernate.hbm2ddl.auto 设置为none 你不想让hibernate 创建你的模式(这需要很长时间,尤其是update 因为它需要检测变化)。您还需要将generetaDdl 属性设置为false。并且还要使用适当的连接池(DriverManagerDataSource 不打算在生产环境中使用)。如果您使用的是组件扫描,请限制需要扫描的包的数量。

标签: mysql spring hibernate tomcat jpa


【解决方案1】:

很简单,不要使用以下属性:

hibernate.hbm2ddl.auto=update

您应该在部署时使用其他工具来迁移生产环境的架构,例如 Flyway 或 Liquibase,而不是 Hibernate 架构工具。

将属性设置为以下值应该会大大缩短您的启动时间。

hibernate.hbm2ddl.auto=none

【讨论】:

  • 我可以删除 hibernate.hbm2ddl.auto=update 并添加 hibernate.hbm2ddl.auto=none 但问题没有解决
  • 那么你需要提供更多关于它慢的地方和应用程序在做什么的细节。
猜你喜欢
  • 1970-01-01
  • 2012-05-03
  • 2018-05-04
  • 2023-03-29
  • 2011-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
相关资源
最近更新 更多