H2 is a database that we use very widely in our topics and projects. In this topic, you will get an overview of its specifics and features. Also, you will find out why it's so popular in Java application development.
What is H2?
H2 is a relational database management system coded in Java. It is an open-source project and it was first released in 2005. You can take a look at its implementation on GitHub. H2 is an SQL database that also supports JDBC API. You can find detailed documentation on the database on its official website.
H2 is mainly used in the development process of applications or for its testing. However, it's never used in large-scale applications for reasons that we'll discuss later in this topic.
H2 main features
First of all, H2 is a complete relational SQL database, so you can create complicated schemas and any SQL queries, like in other popular relational databases. H2 also supports various types of data, including complex ones, such as arrays, as well as several types of data storage.
Another useful feature of the H2 database is that it supports both embedded and server operation modes. That means we can choose an embedded database when we don't want other applications to have access to it. Also, there is no way to get the data from such a database when the application is stopped. The database works significantly faster in this mode.
Additionally, H2 supports some built-in tools. The H2 console is a convenient tool that allows us to interact with the database using a web browser. The H2 Shell is a tool that provides a command-line interface for interacting with the database.
The H2 database supports the SQL standard. However, like all popular relational databases, it has its own dialect. There are a few differences between, for example, MySQL queries and H2 ones. It may become a problem when we want to test the application that uses another database in production. Fortunately, H2 has compatibility modes for the most popular relational databases. They make the syntax of H2 queries closer to those of other SQL databases.
Advantages of H2
The H2 database has several advantages which make it so popular in Java development. Let's take a look at the most important ones:
H2 is very easy to set up. All we need to do is just add the dependency
com.h2databaseto our project and the database is ready to use!The H2 engine is lightweight: its size is only about 2.5 MB!
The performance of the H2 database is one of the best among popular relational databases.
The H2 database is created in Java and made specifically for Java development. So it's easy to integrate it into Java apps that use popular frameworks, such as Spring, Hibernate, and Eclipse.
Disadvantages of H2
Why do we need other relational databases if H2 is so great? Of course, the H2 database also has some disadvantages.
The most important thing we need to know is that the H2 database is not suitable for large-scale enterprise applications. It simply wasn't created for working with them. H2 can't perform so well when it deals with high-concurrency environments with multiple users accessing the database simultaneously. Also, H2 doesn't have many features in comparison with giants such as MySQL or PostgreSQL.
Another thing to consider is that the H2 database is an open-source project with not so large a community as, for example, PostgreSQL. So, technical support is also not its strongest side.
Conclusion
In this topic, you learned that the H2 database is a great choice for the testing and development process of the project. It is fast, lightweight, and easy to set up, and all these qualities make this database perfect for educational purposes. However, the H2 database is not suitable for large-scale applications due to the lack of features for them.