HSQL introduction, hsqldb

Source: Internet
Author: User

HSQL introduction, hsqldb
PrefaceWhen writing a test class to the dao layer, we need a test database. Generally, we will build a real test database, but with HSQLDB, it becomes easier.
QuestionI. Introduction:Hsql database is a free database written in pure Java. The license is the BSD-style protocol. If you are using Java programming, consider using it. Compared with other databases, it is small, only 563kb. Only one hsqldb. jar file includes the database engine, database driver, and other user interface operations. In the Java open-source world, hsql is extremely popular (for Java itself). By default, JBoss application servers also provide this database engine. Because of its small size, it is a pure Java design and supports most of SQL99 and SQL2003 standards, so it is also a choice for commercial application display. Ps: Official http://hsqldb.org/
Ii. Advantages
1. lightweight, with a running speed of more than 600 K. Combined with the features unrelated to the Hibernate database, it is very suitable for use during project development.
2. As a unit test database. During unit testing, the file mode of HSQLDB is started. Data is not stored on the disk, which ensures the test atomicity.
3. copy back and forth and carry it with you.
4. installation is not required, which is very convenient to use.
5. It is stable and supports a large enough data volume.
6. Small projects are used as on-site databases without the need to install large databases such as Oracle, which reduces maintenance costs and makes HSQLDB easy to back up.
Iii. Limitations
1. HSQLDB is not a formal database product. It is inappropriate to use HSQLDB as a commercial application database or a database during development. This is also mentioned in the official HSQLDB documentation.
2. As for the test database, HSQLDB supports standard SQL, so it is normal, but it is not compatible with the special Syntax of a certain data (such as MySql), so it is easy to report errors.
4. Use as a test databaseAs a test database, it is mainly used to test the classes that interact with data, that is, the dao layer we usually talk about. HSQLDB is easy to use. You only need to download the zip package of the latest version from the official website and decompress it to the lib directory (of course, the maven project needs to be added to pom. xml), and then you need a configuration file, two SQL files and a test class.
Test class:

package com.demandforce.dao;import java.util.Collection;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.util.Assert;import com.demandforce.model.TextMessageTemplate;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"classpath:context/hyperSqlContext.xml"})public class TextMessageTemplateRetrievalTest {    @Autowired    private TextMessageTemplateDao textMessageTemplateDao;                @Test    public void testSelectByBusinessCategory() {        Collection<TextMessageTemplate> tmts = textMessageTemplateDao.selectByBusinessCategory( 203, 0);        Assert.notNull(tmts);        Assert.isTrue(tmts.size() == 2);    }  }



HyperSqlContext. xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:jdbc="http://www.springframework.org/schema/jdbc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">    <jdbc:embedded-database id="dataSource" type="HSQL">        <jdbc:script location="classpath:sql/setup.sql" />        <jdbc:script location="classpath:sql/schema-create.sql" />        <jdbc:script location="classpath:sql/data-insert.sql" />    </jdbc:embedded-database>    <bean id="transactionManager"        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    </bean>    <bean name="baseDao" abstract="true">        <property name="dataSource" ref="dataSource" />    </bean>  <bean name="textMessageTemplateDao" class="com.demandforce.dao.TextMessageTemplateDaoImpl" parent="baseDao" /></beans>



Setup. SQL
---- MySQL compatibility mode for Hyper SQLSET DATABASE SQL SYNTAX MYS TRUE;



Schema-create. SQL
DROP TABLE IF EXISTS TextMessageTemplate;CREATE TABLE TextMessageTemplate (  ID INT NOT NULL AUTO_INCREMENT,  BusinessID INT NOT NULL,  Type INT NOT NULL,  Template varchar(1000),  CreatedDate datetime DEFAULT NULL,  LastModifiedDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,  CreatedUserId INT DEFAULT 0,  LastModifiedUserId INT DEFAULT 0,  Delivery INT DEFAULT 0,  DeliveryWindow INT DEFAULT 1,  BusinessCalendar INT NOT NULL DEFAULT 0,  DeliveryTime datetime DEFAULT NULL,  CardCount INT DEFAULT 0,  Segment INT DEFAULT 0,  FrontImage varchar(255) DEFAULT NULL,  IncludeItems INT DEFAULT 1,  IncludeReview INT DEFAULT 1,  IncludeCoupon INT DEFAULT 1,  services varchar(5000) DEFAULT NULL,  Industry INT DEFAULT NULL,  PRIMARY KEY (ID));



Data-insert. SQL
INSERT INTO TextMessageTemplate (BusinessID, Type, Template, Industry) VALUES ('0', '203', 'Template1', null);INSERT INTO TextMessageTemplate (BusinessID, Type, Template, Industry)VALUES ('0', '204', 'Template2', null);



Conclusion: Although HSQLDB has some limitations, it is a good choice for testing databases in some cases.

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.