繁体   English   中英

HikariPool- 池初始化期间的异常

[英]HikariPool- Exception during pool initialization

我正在创建一个 spring 引导项目。 然后我尝试将 mysql 数据库连接到该项目。 我收到名为“HikariPool-1 - 池初始化期间的异常”的错误。

这是整个堆栈跟踪。

2022-01-15 11:09:29.374  INFO 2748 --- [nio-8888-exec-3] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...

2022-01-15 11:09:30.481 ERROR 2748 --- [nio-8888-exec-3] com.zaxxer.hikari.pool.HikariPool: HikariPool-1 - Exception during pool initialization.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]

2022-01-15 11:09:30.490 ERROR 2748 --- [nio-8888-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 

### Error querying database.  Cause:org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in com/example/reservation/mapper/userMapper.java (best guess)
### The error may involve com.example.reservation.mapper.userMapper.findAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)] with root cause
My is as follows,application.yml

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.27.jar:8.0.27]

我的如下,application.yml

server:
      port: 8888
spring:
      datasource:
        url: jdbc:mysql://127.0.0.1:3306/reservation?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
        username: root
        password: "123456"
        driver-class-name: com.mysql.cj.jdbc.Driver

我的依赖是,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>reservation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>reservation</name>
    <description>reservation project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

用户.java

@Data
@AllArgsConstructor
@NoArgsConstructor
public class user {
    private Integer id;
    private String username;
    private String password;
}

userMapper.java

public interface userMapper {
    @Select("select * from user")
    List<user> findAll();
}

userMapper.java

public class userController {

   @Resource
   userMapper usermapper;       
   @GetMapping()              
   public String hello(){
      return "hello";
   }
   @GetMapping("/user")          
   public List<user> getUser(){
        return usermapper.findAll();
   }

}

预订申请.java

@SpringBootApplication
@MapperScan("com.example.reservation.mapper")
public class ReservationApplication {
    public static void main(String[] args) {
        SpringApplication.run(ReservationApplication.class, args);
    }
}

例外

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server
......
Caused by: java.net.ConnectException: Connection refused: connect
WARN 17972 --- [nio-8888-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 08S01
ERROR 17972 --- [nio-8888-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
ERROR 17972 --- [nio-8888-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause

java.net.ConnectException: Connection refused: connect

当我访问 localhost:8888 时,它可以成功运行,

但是当我访问 localhost:8888/user 时出现错误

发生了什么?:(

您必须在 POJO 中添加Repositoy接口。 它可以使用JpaRepository<>扩展,之后您可以使用 spring 数据 jpa 的所有属性。

First you need to have knowledge about java naming convention https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html .

user user User反向关键字。 在这里,我可以向您展示如何制作存储库。

移除mybatis依赖,添加Spring Data Jpa依赖 From https://mvnrepository.com/artifact2/org.spring-j-starter/booter2.6

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

卸妆userMapper并创建UserRepo

package pkg-name;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepo extends JpaRepository<User, Integer>{

}

现在你的Controller变成如下

@RestController
public class UserController {

   @Autowired
   private UserRepo userRepo; 
      
   @GetMapping()              
   public String hello(){
      return "hello";
   }

   @GetMapping("/user")          
   public List<User> getUser(){
        return userRepo.findAll();
   }

}

ReservationApplication.java中删除@MapperScan("com.example.reservation.mapper")注释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM