H2 memory databases support file storage,

Source: Internet
Author: User

H2 memory databases support file storage,
Zookeeper

Preparations

1. Download JDK (my downloaded version is JDK 1.7), set the environment variable JAVA_HOME, and set PATH (% JAVA_HOME % \ bin % ).
2, download and unzip: h2-2014-07-13.zip Official Website: http://www.h2database.com/html/main.html
3. Set the environment variable H2_HOME. % H2_HOME % indicates the decompressed file directory. Running % H2_HOME % \ bin \ h2.bat will automatically open the following URL. (Check whether jdk is installed and set the JAVA_HOME environment variable)
Http: // localhost: 8082/login. jsp? Jsessionid = 244e36a683f97f0d4f3b000f33530ed1

3. Click connect to log on.

4. Execute the red SQL statement to successfully create the test table.

Because no database file location is specified, it is automatically output to C: \ Users \ Administrator.

H2 file structure

 
% H2_HOME %
-H2
-Bin
H2-1.3.154.jar // jar package
H2.bat // Windows console Startup Script
H2.sh // Linux console Startup Script
H2w. bat // Windows console STARTUP script (without a black screen window)
+ Docs help documentation
+ Service // wrap the package into a service through wrapper.
+ Src // source code
Build. bat windows build script
Build. sh linux build script

H2 usage
Embedded, server, in-memory, and memory modes are supported.
Embedded Mode

1. Create a java project H2Test.

 

2. Copy % H2_HOME % \ bin \ h2-1.3.154.jar to \ H2Test \ lib and add the project reference.
3. Create a Generic H2 (Embedded) database, specify jdbc url: jdbc: h2: E: \ research \ workspace \ H2Test \ db \ test, and then execute the preceding test SQL statement, create a test table.

4. Create the main code of the TestH2 class

Public static void main (String [])
Throws Exception {
Class. forName ("org. h2.Driver ");
Connection conn = DriverManager.
GetConnection ("jdbc: h2: E: \ research \ workspace \ H2Test \ db \ test", "sa ","");
// Add application code here
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("SELECT * from test ");
While (rs. next ()){
System. out. println (rs. getInt ("ID") + "," + rs. getString ("NAME "));
}
Conn. close ();
}

 
Output in the console: 1, Hi

Server Mode

1. Change the jdbc url to jdbc: h2: tcp: // localhost /~ /Test. Because we have created the test database in C: \ Users \ Administrator at the first step.
You can also create a new database, which is saved under C: \ Users \ Administrator by default.
Note: You must start the service: % H2_HOME % \ bin \ h2.bat or start it in service mode: % H2_HOME % \ service \ 0_run_server_debug.bat. Several scripts in it deploy H2 in service mode. After each machine is started, the H2 service is automatically started.
2. Create the main code of the TestServerH2 class

Public static void main (String [])
Throws Exception {
Class. forName ("org. h2.Driver ");
Connection conn = DriverManager.
GetConnection ("jdbc: h2: tcp: // localhost /~ /Test "," sa ","");
// Add application code here
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("SELECT * from test ");
While (rs. next ()){
System. out. println (rs. getInt ("ID") + "," + rs. getString ("NAME "));
}
Conn. close ();
}

The running result is the same as above.

Memory mode (data is only stored in memory)

1. Create the main code of the TestMemH2 class

Public static void main (String [])
Throws Exception {
Class. forName ("org. h2.Driver ");
Connection conn = DriverManager.
GetConnection ("jdbc: h2: tcp: // localhost/mem: test2", "sa ","");
// Add application code here
Statement stmt = conn. createStatement ();

Stmt.exe cuteUpdate ("create table TEST_MEM (id int primary key, name varchar (255 ));");
Stmt.exe cuteUpdate ("insert into TEST_MEM VALUES (1, 'Hello _ Mem ');");
ResultSet rs = stmt.exe cuteQuery ("SELECT * FROM TEST_MEM ");
While (rs. next ()){
System. out. println (rs. getInt ("ID") + "," + rs. getString ("NAME "));
}
Conn. close ();
}

The console prints: 1, Hello_Mem

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Cluster/High Availability

The Database supports a simple cluster/High Availability mechanism. The architecture is: two database services run on two different computers, and two computers have copies of the same database. If both servers are running, each database operation is performed on two computers. If one server is down (power failure, hardware failure, network failure, etc.), the other computer can still provide services. From this moment on, database operations are performed on only one server until the other server resumes operation.

Clusters can only be used in server mode (embedded mode does not support clusters ). The cluster can be restored when the database is running, but no application is required to change the data of the first database during the recovery of the second database. Therefore, restoring the cluster is a manual process.

To initialize a cluster, follow these steps:

· Create a database

· Use the CreateCluster tool to create a database and initialize the cluster. In this way, the two databases with the same data are obtained.

· Start two database services (copies of each database)

· Now you can connect to the database through the Application Client

Use the cluster creation tool

To learn how clusters work, try the following example. In this example, two databases reside on the same computer, but usually two databases reside on different computers.

· Create two directories: server1 and server2. Each directory simulates a computer.

· Start the TCP Service in the first directory. You can run the following command:

· Java org. h2.tools. Server

·-Tcp-tcpPort 9101

·-BaseDirserver1

· Start the TCP Service in the second directory and simulate the second server (run in redundancy). You can run the following command:

· Java org. h2.tools. Server

·-Tcp-tcpPort 9102

·-BaseDirserver2

· Use the CreateCluster tool to initialize the cluster. If the database does not exist, create a new empty database and run the following command:

· Java org. h2.tools. CreateCluster

·-UrlSourcejdbc: h2: tcp: // localhost: 9101 /~ /Test

·-UrlTargetjdbc: h2: tcp: // localhost: 9102 /~ /Test

·-User sa

·-ServerList localhost: 9101, localhost: 9102

· The application or H2 console can connect to the database through the following jdbc url: jdbc: h2: tcp: // localhost: 9101, localhost: 9102 /~ /Test

· If you stop a service (by killing the process) and you notice that another machine continues to work, the database can still provide access.

· To restore a cluster, delete the database with downtime, restart the service of the database with downtime, and run the CreateCluster cluster tool again.

Detect clusters in running status

Find which nodes are currently running and execute the following SQL statement:

Select value from INFORMATION_SCHEMA.SETTINGS WHERENAME = 'cluster'

The returned result is ''(two single quotes), indicating that the cluster mode is blocked. Otherwise, the Cluster Server list is enclosed in single quotes and returned, for example, 'server1: 9191, server2: 100 '.


2. Change the URL above to jdbc: h2 :~ /Mem: test is also possible. If it is localhost, the service must be started.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Database Connection URL description


The Database supports multiple connection modes and connection settings. Different connection modes and connection settings are differentiated by different URLs. The URL settings are case insensitive.

Topic

URL Format and Examples

Embedded (local) Connection

Jdbc: h2: [file:] [<path>] <databaseName>
Jdbc: h2 :~ /Test
Jdbc: h2: file:/data/sample
Jdbc: h2: file: C:/data/sample (Windows only)

Memory Database (private)

Jdbc: h2: mem:

Memory Database (named)

Jdbc: h2: mem: <databaseName>
Jdbc: h2: mem: test_mem

Server mode using TCP/IP (remote connection)

 

Jdbc: h2: tcp: // <server> [: <port>]/[<path>] <databaseName>
Jdbc: h2: tcp: // localhost /~ /Test
Jdbc: h2: tcp: // dbserv: 8084 /~ /Sample

Server mode using SSL/TLS (remote connection)

 

Jdbc: h2: ssl: // <server> [: <port>]/<databaseName>
Jdbc: H2. ssl: // secureserv: 8085 /~ /Sample;

Use Encrypted Files

Jdbc: h2: <url>; CIPHER = [AES | XTEA]
Jdbc: h2: ssl: // secureserv /~ /Testdb; CIPHER = AES
Jdbc: h2: file :~ /Secure; CIPHER = XTEA

File lock

Jdbc: h2: <url>; FILE_LOCK = {NO | FILE | SOCKET}
Jdbc: h2: file :~ /QuickAndDirty; FILE_LOCK = NO
Jdbc: h2: file :~ /Private; CIPHER = XTEA; FILE_LOCK = SOCKET

Only open existing databases

Jdbc: h2: <url>; IFEXISTS = TRUE
Jdbc: h2: file :~ /Sample; IFEXISTS = TRUE

The database is not closed when the VM exits.

Jdbc: h2: <url>; DB_CLOSE_ON_EXIT = FALSE

Username and password

Jdbc: h2: <url> [; USER = <username>] [; PASSWORD = <value>]
Jdbc: h2: file :~ /Sample; USER = sa; PASSWORD = 123

Update to index

Jdbc: h2: <url>; LOG = 2
Jdbc: h2: file :~ /Sample; LOG = 2

Debug tracking item settings

Jdbc: h2: <url>; TRACE_LEVEL_FILE = <level 0... 3>
Jdbc: h2: file :~ /Sample; TRACE_LEVEL_FILE = 3

Ignore location parameter settings

Jdbc: h2: <url>; IGNORE_UNKNOWN_SETTINGS = TRUE

Specify file read/write mode

Jdbc: h2: <url>; ACCESS_MODE_LOG = rws; ACCESS_MODE_DATA = rws

Databases in the Zip file

Jdbc: h2: zip: <zipFileName>! /<DatabaseName>
Jdbc: h2: zip :~ /Db.zip! /Test

Compatibility mode

Jdbc: h2: <url>; MODE = <databaseType>
Jdbc: h2 :~ /Test; MODE = MYSQL

Automatic Reconnection

Jdbc: h2: <url>; AUTO_RECONNECT = TRUE
Jdbc: h2: tcp: // localhost /~ /Test; AUTO_RECONNECT = TRUE

Automatic hybrid mode

Jdbc: h2: <url>; AUTO_SERVER = TRUE
Jdbc: h2 :~ /Test; AUTO_SERVER = TRUE

Change other settings

Jdbc: h2: <url >;< setting >=< value> [; <setting >=< value>...]
Jdbc: h2: file :~ /Sample; TRACE_LEVEL_SYSTEM_OUT = 3




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.