如何减少Spring Jpa Hibernate项目部署时间?

时间:2017-02-28 05:19:21

标签: mysql spring hibernate tomcat jpa

我有一个开发spring,jpa和hibernate以及数据库mysql和服务器Apache Tomcat 8的web项目。我的项目大小为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服务器在短时间内快速部署我的项目。

由于

1 个答案:

答案 0 :(得分:0)

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

hibernate.hbm2ddl.auto=update

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

将属性设置为以下内容可以大大缩短启动时间。

hibernate.hbm2ddl.auto=none