首页 > 解决方案 > Spring-boot Web 项目无法插入/保存/更新 Wildfly 容器的数据库查询,但适用于 Tomcat 容器

问题描述

我在用着

依赖(pom.xml)

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-jdbc</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

配置

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
    import org.springframework.orm.jpa.JpaVendorAdapter;
    import org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean;
    
    import javax.sql.DataSource;
    import java.util.Properties;
    
    @Configuration
    public class SessionFactoryConfig {
    
        @Autowired
        DataSource dataSource;
    
        @Autowired
        JpaVendorAdapter jpaVendorAdapter;
    
    
        @Bean
        public LocalSessionFactoryBean sessionFactory() {
            LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
            sessionFactory.setPackagesToScan("com.example.abc");
            sessionFactory.setDataSource(dataSource);
            sessionFactory.setHibernateProperties(hibernateProperties());
    
    
            return sessionFactory;
        }
    
    private Properties hibernateProperties() {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty("hibernate.ddl.auto", "none");
        hibernateProperties.setProperty("hibernate.connection.autocommit", "true");
        hibernateProperties.setProperty("hibernate.connection.release_mode", "auto");  
        hibernateProperties.setProperty("hibernate.show_sql", "true");
        hibernateProperties.setProperty("hibernate.format_sql", "true");
        hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
        hibernateProperties.setProperty("hibernate.current_session_context_class", 
        "org.springframework.orm.hibernate5.SpringSessionContext");        
        return hibernateProperties;
    }
    }

应用程序属性

spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.hikari.maximum-pool-size=100


spring.datasource.tomcat.max-active=80
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.initial-size=20
spring.datasource.tomcat.time-between-eviction-runs-millis=60005
spring.datasource.tomcat.validation-query:"SELECT 1"

spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.remove-abandoned-on-borrow=true
spring.datasource.dbcp2.remove-abandoned-timeout=30
 

不适用于 wildfly,但适用于 Tomcat

public class entityDao{
    @Autowired
    SessionFactory sessionFactory;
    public  void update(Entity1 entity1){

      this.sessionFactory.getCurrentSession().update(entity1);
    }
}

 

如果我更换

this.sessionFactory.getCurrentSession().update(entity1);

this.sessionFactory.getCurrentSession().getTransction.begin();
this.sessionFactory.getCurrentSession().update(entity1);
this.sessionFactory.getCurrentSession().getTransction.commit();

然后它在野蝇中工作正常。

所有 SELECT Query 在两个容器中都可以正常工作。更新/插入查询中的唯一问题。

标签: mysqlspring-boothibernatespring-mvcwildfly

解决方案


我得到了这个解决方案。

  • 删除 SessionFactoryConfig.java

  • 在 Application.java 文件中使用 @EnableJpaAuditing。

推荐阅读