Can’t reach H2 database from Web application (error code: [42104-224])

  Kiến thức lập trình

I created a web application where I try to do some filtering after a POST request. I get the following error messages:

Table “users” not found (this database is empty); SQL statement:

SELECT * FROM users WHERE login = ? [42104-224]

The whole thing is backed with a basic DAO structure, it is about users and roles (these are the table names, too). When I try it as a simple application from main then it finds the database. Unlike web application where I get the above-mentioned message.

I use this Dockerfile:

FROM maven:3.9.7-eclipse-temurin-11 AS builder

WORKDIR /app

COPY . /app

RUN mvn clean package

FROM tomcat:9.0.89-jre11-temurin-jammy

COPY --from=builder /app/target/*.war $CATALINA_HOME/webapps/ROOT.war

EXPOSE 8080

CMD ["catalina.sh", "run"]

and this docker-compose.yml:

services:
  app:
    build: .
    ports:
      - 8080:8080
    depends_on:
      - db
    container_name: m-application
  db:
    image: oscarfonts/h2:2.2.224
    container_name: m-database
    restart: no
    environment:
      H2_URL: jdbc:h2:file:/opt/h2-data/servlets_jsp.mv.db;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1;
      H2_OPTIONS: '-tcp'
    ports:
      - 81:81
    volumes:
      - ./servlets_jsp.mv.db:/opt/h2-data/servlets_jsp.mv.db:ro

There is a prepared database and the CRUD methods use the same database url:

jdbc:h2:file:./servlets_jsp;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE;

In LoginServlet.java I try to reach my DB this way:

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String login = request.getParameter("login");
        String password = request.getParameter("password");

        UserDao userDao = JdbcUserDao.getInstance();
        User user = null;
        try {
            user = userDao.findByLogin(login);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }

        if(user == null){
            response.sendRedirect("/?error=true");
        } else if (user.getRoleId() == 1) {
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/admin_home.jsp").forward(request, response);
        } else if(user.getRoleId() == 2){
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            request.getRequestDispatcher("/WEB-INF/user_home.jsp").forward(request, response);
        }
    }

The PreparedStatement used by findByLogin uses this statement:

"SELECT * FROM users WHERE login = ?"

In webconsole at http://localhost:81/ I can reach my table and can query the tables with the preloaded data.

I’m stuck here very well. Do you have some ideas how could I find my mistake here? If I can provide any relevant thing that could bring the solution closer, please let me know.

Thank you very much for your help in advance.

LEAVE A COMMENT