简体   繁体   中英

Spring ERR_CONNECTION_REFUSED

I am following a tutorial on spring, and so far my code allows you to add a user and view all users. I am now trying to include delete functionality. I added the below code to achieve this. I believe it is exactly as shown in the tutorial, but as a result I get ERR_CONNECTION_REFUSED. In the tutorial this error showed initially but upon refresh went away, but in my case it does not.

index.html

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

userService.java

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

userController.java

@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;
}

application.properties (the bit added to this line is ;FILE_LOCK=FS )

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

I noticed in spring boot I now get the following, which I know references the lock method I just added for deleting, but not sure if this is why I am getting connection refused as the tutorial did the same thing but worked:

2018-01-24 16:18:54.446 WARN 6589 --- [ restartedMain] ohejeiJdbcEnvironmentInitiator : 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] osboot.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]

UPDATE

Documentation for h2 database says this about file lock, so why would this be an unsupported method?:

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.

Spring Boot application startup seems to be failed. Your sever is not started. As per the logs, creation of bean entityManagerFactory failed. This because, FS is an Unsupported file lock method . Kindly check if this is valid and is what you need. Try removing this and check if it works.

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

Just incase someone else encounters the same problem, adding the third line fixes this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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