You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

H2数据库服务器/混合模式异常:App1更新数据App2无法查看

Troubleshooting H2 Server Mode Data Visibility Issue

Hey there! Let's work through why your App1 updates aren't showing up in App2 when using H2's server mode. From your description, the root issue is likely how you're initializing and connecting to the database across different processes.

What's Going Wrong?

When you create the database via the H2 Console using the embedded connection string jdbc:h2:~/maBaseH2FichierTCP, that database file is locked and managed exclusively by the Console process. Your Java app connecting via jdbc:h2:tcp://localhost:9092/~/maBaseH2FichierTCP is either connecting to a separate instance of the database (if the H2 Server is running under a different user, where ~/ points to a different directory) or can't properly access the file locked by the Console.

Step-by-Step Fix

Let's reset your setup to ensure all clients connect through the H2 Server properly:

  1. Clean up existing files

    • Shut down all H2-related processes (Console, Java apps) completely.
    • Delete all files starting with maBaseH2FichierTCP (including .mv.db, .trace.db, and lock files) from your user directory (~/).
  2. Start the H2 Server explicitly
    Open a terminal, navigate to where your H2 jar file is located, and run this command to start the TCP server:

    java -cp h2*.jar org.h2.tools.Server -tcp -tcpPort 9092
    

    This starts the H2 Server process that will manage your database file, so no single client locks it exclusively.

  3. Connect H2 Console to the Server

    • Open the H2 Console in your browser.
    • Use this connection string instead of the embedded one:
      jdbc:h2:tcp://localhost:9092/~/maBaseH2FichierTCP
      
    • Enter username sa and leave the password blank, then click "Connect". Create your database and tables here—now the database is managed by the H2 Server, not the Console process.
  4. Update your Java app's connection string
    Ensure your Java application uses the exact same connection string as the Console:

    String url = "jdbc:h2:tcp://localhost:9092/~/maBaseH2FichierTCP";
    String user = "sa";
    String password = "";
    Connection conn = DriverManager.getConnection(url, user, password);
    
  5. Verify transaction behavior
    Double-check that your Java apps are committing transactions after updates. If App1 doesn't call conn.commit() (or uses auto-commit disabled without committing), changes won't be visible to other connections. You can enable auto-commit with:

    conn.setAutoCommit(true);
    

Additional Checks

  • Match H2 versions: Make sure the H2 jar used by the Server, Console, and your Java apps are all the same version. Version mismatches can cause unexpected data visibility issues.
  • Check file permissions: Ensure the user running the H2 Server has read/write access to your ~/ directory where the database file is stored.

内容的提问来源于stack exchange,提问作者Xavier Marquis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:54:15