H2 is database engine (RDBMS) base on Java. Its features includes in memory database, embeded database as file database. In Wildfly, H2 is installed as default in the application server. In this post, I will show how to connect to H2 database from application and use H2 as an in memory database and file database.
The content of this post followed as below
- Connect to H2 datasource
- CRUD Operation on Wildfly
- Use H2 as File Database
- Reference
For my environment setup, I have used
- Application Server : Widlfly 23.0.2
- JDK 1.11 LTS
1. Connect to H2 Datasource
H2 Datasource come in default with Wildfly. If you want to try to use in memory databse, Wildfly provide provide H2 for you to use in the instance. After start Wildfly, you can login to the management console and you shall find below.
You could try to connect to the database. Once you check the connection, you should receive as below
Now you are ready to start read and write to H2 Database
2. CRUD Operation with JAVA EE
Once you confirmed that your datasource is set up properly, you could create JPA project. In your persistence.xml, jta-data-source tag specify H2 datasource JNDI name as below.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.2" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd">
<persistence-unit name="sample-h2-jpa-02">
<jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
One more important point is you need to set hibernate.hbm2ddl.auto value as update
Next is to create Entity class. for my example, I have create simple entity class as below
package pro.tikkwiki.jpa.sample.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String tel;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
}
Then, you could create EJB Project to implement DAO classes. And after creating DAO, you could create interface by Dynamic Web Project.
3.Using H2 Database as a File Database
Memory Database provide very fast access for the application. But one of the drawback point of memory database is volatile. Once you restart your application server or reboot OS. all of the data persisted is disappeared.
To encounter this disadvantage of in memory database, H2 also provide file database to prevent data volatile once restart application or platform. To create file database in H2, it is very simple. Just create new datasource and specify url from mem to file. For example, you need to set up url for connection as
dbc:h2:file:D:\Devs\h2\sampledb;DB_CLOSE_DELAY=-1
No comments:
Post a Comment