Keywords: H2 Database | Server Mode | Database Configuration
Abstract: This article provides an in-depth exploration of H2 database server mode configuration, focusing on the analysis of common 'database locked' errors and their solutions. It explains the different connection modes of H2 database, including embedded mode, server mode, and automatic mixed mode, detailing their distinctions and appropriate use cases. Through code examples and configuration instructions, the article guides developers in correctly configuring H2 database servers, avoiding conflicts caused by simultaneous use of AUTO_SERVER=TRUE parameter and manual server startup. Practical methods for starting H2 servers from the command line are also provided to help developers quickly set up database environments.
Overview of H2 Database Connection Modes
H2 database, as a lightweight Java database, offers multiple connection modes to meet various application requirements. Understanding the differences between these modes is crucial for proper configuration and usage of H2 database.
Detailed Explanation of Main Connection Modes
H2 database supports three primary connection modes: embedded mode, server mode, and automatic mixed mode. Embedded mode is the simplest approach where the database engine runs directly within the application process, suitable for single-user or testing environments. Server mode allows the database to run as an independent service, supporting multiple concurrent client connections, ideal for production environments or multi-user scenarios. Automatic mixed mode is a special configuration where a TCP server automatically starts when the database is opened in embedded mode, allowing other processes to connect.
Common Error Analysis and Solutions
In practical development, a frequent error occurs when both server mode and automatic mixed mode are enabled simultaneously, leading to database locking conflicts. The error message typically displays: Database may be already in use: "Locked by another process". The core cause of this error is H2 database's file locking mechanism preventing multiple processes from accessing the same database file in embedded mode concurrently.
When the configuration includes both the AUTO_SERVER=TRUE parameter and code to start a TCP server, two independent database instances attempt to access the same database file. The first instance (possibly an embedded connection) has already locked the database file, preventing the second instance (TCP server) from obtaining the file lock, thus throwing an exception.
Correct Server Mode Configuration
To properly configure H2 database server mode, one must clearly choose a single connection approach. If opting for manual server startup, use the following code:
import org.h2.tools.Server;
public class H2ServerExample {
public static void main(String[] args) throws Exception {
// Start TCP server
Server server = Server.createTcpServer(
"-tcpPort", "9092",
"-tcpAllowOthers"
).start();
System.out.println("H2 TCP server started");
}
}
The corresponding database connection URL should be configured as:
jdbc:h2:tcp://localhost:9092/~/source/db/database/db
Note that this configuration does not include the AUTO_SERVER=TRUE parameter. This approach clearly distinguishes between server and client roles, avoiding file locking conflicts.
Command Line Startup Method
In addition to Java code startup, H2 database provides command line startup options suitable for quick testing and development environment setup:
java -jar h2-1.3.160.jar -webAllowOthers -tcpAllowOthers
After executing this command, the console displays information similar to:
Web Console server running at http://A.B.C.D:8082 (others can connect)
TCP server running at tcp://A.B.C.D:9092 (others can connect)
PG server running at pg://A.B.C.D:5435 (only local connections)
This method simultaneously starts both the Web console and TCP server, facilitating database management through a browser.
Configuration Considerations
When configuring H2 database connections, several key points require attention:
- Avoid Mode Conflicts: Do not use both
AUTO_SERVER=TRUEand manually started TCP servers simultaneously. - File Path Handling: Ensure correct database file paths, especially when using relative paths.
- Permission Configuration: In production environments, configure access permissions appropriately to avoid security risks.
- Connection Pool Configuration: When using connection pools, ensure connection URLs correctly point to TCP server addresses.
Best Practice Recommendations
Based on practical development experience, we recommend:
- Use embedded mode or automatic mixed mode in development environments for rapid testing.
- Employ standalone server mode in production environments to ensure stability and concurrency support.
- Regularly backup database files, particularly when using file storage mode.
- Monitor database connection counts to prevent resource exhaustion.
By correctly understanding H2 database connection modes and working principles, developers can avoid common configuration errors and build stable, reliable database applications.