Staaankey
Staaankey

Reputation: 155

How to not lose data after reloading the server?

Devs! To pass the test I should get some snippets of code and return them by RESTful API requests. I`ve done with the first part of the test and sent all snippets of code to the H2 database. In the second part of testing my program, they are reloading server by the command this::reloadServer. How I can get my data back from the database to successfully respond to RESTful requests and pass the test?

My controller:

@Controller
public class SnippetController {

    @Autowired
    SnippetService snippetService;
    @Autowired
    Repository repository;


    public SnippetController(SnippetService snippetService) {
        this.snippetService = snippetService;
    }

    @GetMapping(value = "/api/code/{id}")
    @ResponseBody
    public Snippet getSnippet(@PathVariable int id) {
        return snippetService.getSnippet(id);
    }

    @GetMapping(value = "/code/{id}")
    public String showSnippet(@PathVariable int id, Model model) {
        model.addAttribute("snippet", snippetService.getSnippet(id));
        return "code";
    }

    @GetMapping(value = "/api/code/latest")
    @ResponseBody
    public List<Snippet> getLatestSnippets() {
        return snippetService.getLatestSnippets();
    }

    @GetMapping(value = "/code/latest")
    public ModelAndView showLatestSnippets(Model model) {
        List<Snippet> sortedList = getLatestSnippets();
        ModelAndView mv = new ModelAndView("latest");
        mv.addObject("latestSnippets", sortedList);
        return mv;
    }

    @PostMapping(value = "/api/code/new")
    @ResponseBody
    public Map<String, String> addSnippet(@RequestBody Snippet snippet) {
        int id = snippetService.addSnippet(snippet);
        repository.save(snippet);
        return Map.of("id", String.valueOf(id));
    }

    @GetMapping(value = "/code/new")
    public String showSnippetAddingForm() {
        return "create";
    }
}

application.properties

server.port=8888
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
spring.datasource.url=jdbc:h2:file:../snippetsOfCode
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update

spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

stacktrace

How I should modify my controller to do this?

Upvotes: 0

Views: 648

Answers (1)

Ventrue
Ventrue

Reputation: 391

Well, by default the H2 DB data is volatile, so data will be lost after the restart. What you can do is change H2 to use file-based storage. You don't need to change the controller, but in you can accomplish that in Spring Configuration file:

spring.datasource.url=jdbc:h2:file:/data/demo

or (if using .yml):

spring:
  datasource:
    url: jdbc:h2:file:/data/demo

If that is not working, maybe your DB is being re-created after the restart. You can also add that to your application.properties:

spring.jpa.hibernate.ddl-auto=update

Also check if you have the below line in your persistence.xml:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

If so, remove it.

Upvotes: 1

Related Questions