gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:26:34 25 4
gpt4 key购买 nike

我有一个开发 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 服务器在短时间内快速部署我的项目。

谢谢

最佳答案

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

hibernate.hbm2ddl.auto=update

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

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

hibernate.hbm2ddl.auto=none

关于mysql - 如何减少Spring Jpa Hibernate项目部署时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42500756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com