有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring错误连接被拒绝

我正在学习关于spring的教程,到目前为止,我的代码允许您添加一个用户并查看所有用户。我现在尝试加入删除功能。我添加了下面的代码来实现这一点。我相信这和教程中所示的一模一样,但结果我犯了错误。在本教程中,这个错误最初显示出来,但在刷新时消失了,但在我的例子中没有

索引。html

<a href="#" th:href="@{/user/delete/}+${user.id}">Delete</a>

用户服务。爪哇

public void delete(User user) {userRepository.delete(user); }

用户控制器。爪哇

@RequestMapping(value = "/delete/{user}", method = RequestMethod.GET)
@ResponseBody
public String delete(@PathVariable User user)
{
    String name = user.getFirstname()+" "+user.getLastname();
    userService.delete(user);
    return name;
}

申请。属性(添加到此行的位为;FILE_LOCK=FS

spring.datasource.url=jdbc:h2:file:./database.h2;FILE_LOCK=FS 

我注意到,在spring boot中,我现在得到了以下信息,我知道这些信息引用了我刚刚添加的用于删除的锁定方法,但不确定这是否是我被拒绝连接的原因,因为教程做了同样的事情,但仍然有效:

2018-01-24 16:18:54.446 WARN 6589 --- [ restartedMain] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : Unsupported file lock method "FS " [90060-196] 2018-01-24 16:18:54.447 WARN 6589 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 2018-01-24 16:18:54.447 INFO 6589 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat] 2018-01-24 16:18:54.454 INFO 6589 --- [ restartedMain] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2018-01-24 16:18:54.459 ERROR 6589 --- [ restartedMain] o.s.boot.SpringApplication : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

更新

h2数据库的文档中提到了文件锁,那么为什么这是一种不受支持的方法呢

File Locking Method 'FS' This is the default mode for version 1.4 and newer. This database file locking mechanism uses native file system lock on the database file. No *.lock.db file is created in this case, and no background thread is started. This mechanism may not work on all systems as expected. Some systems allow to lock the same file multiple times within the same virtual machine, and on some system native file locking is not supported or files are not unlocked after a power failure.

To enable this feature, append ;FILE_LOCK=FS to the database URL.

This feature is relatively new. When using it for production, please ensure your system does in fact lock files as expected.


共 (2) 个答案

  1. # 1 楼答案

    spring.datasource.driver-class-name=org.h2.Driver
    
    spring.datasource.url=jdbc:h2:file:./database.h2;FILE_LOCK=FS
    
    spring.jpa.hibernate.ddl-auto=update
    

    如果其他人遇到同样的问题,添加第三行可以解决这个问题

  2. # 2 楼答案

    Spring Boot应用程序启动似乎失败了。你的服务器没有启动。根据日志,创建bean entityManagerFactory失败。这是因为, FS是一个Unsupported file lock method。请检查这是否有效,这是你需要的。试着移除这个并检查它是否有效