2 Şubat 2023 Perşembe

JDBC H2 Connection String

CASE_INSENSITIVE_IDENTIFIERS
Şöyle yaparız
jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;
  DATABASE_TO_LOWER=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE
DATABASE_TO_LOWER 
Veri tabanı ve sütun isimleri kendiliğinden küçük harf haline getiriliyor.

DATABASE_TO_UPPER
Açıklaması şöyle. Bu genelde FALSE yapılıyor. Böylece veri tabanı ve sütun isimleri kendiliğinden büyük harf olmuyor.
The most important thing in H2 is to understand the case sensitivity. It expects exact case in queries, otherwise it will throw error, such as "Table not found" or "Column not found".

By default H2 creates all the tables and columns in upper case letters. such as Create Table userinfo(info int); will be created as USERINFO and the column name in it will be INFO. Two ways to avoid this upper upper case problem.
    a. Using square bracket.
    b. Appending the following in the connection string.
DATABASE_TO_UPPER=FALSE;

Apart from tables and columns, the data in H2 is also case sensitive by default. To avoid case sensitivity, there are two options. IgnoreCase=True option in connection string.
Using collation. SET COLLATION ENGLISH STRENGTH PRIMARY;
Açıklaması şöyle
When set to true unquoted identifiers and short name of database are converted to upper case.
Örnek
Şöyle yaparız
jdbc:h2:mem:mydb;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1
DB_CLOSE_DELAY
Varsayılan değeri 0. Açıklaması şöyle. Eğer -1 ise tüm JDBC bağlantıları kapansa bile veri tabanı açık kalır
By default, closing the last connection to a database closes the database. For an in-memory database, this means the content is lost. To keep the database open, add ;DB_CLOSE_DELAY=-1 to the database URL. To keep the content of an in-memory database as long as the virtual machine is alive, use jdbc:h2:mem:test;DB_CLOSE_DELAY=-1. This may create a memory leak, when you need to remove the database, use the SHUTDOWN command.
Açıklaması şöyle
It's worth noting that setting DB_CLOSE_DELAY to a negative value (e.g., -1) is equivalent to setting DB_CLOSE_ON_EXIT to true. In other words, if you set DB_CLOSE_DELAY=-1, the database will be closed when the last connection to the database is closed or when the JVM exits, just like if you had set DB_CLOSE_ON_EXIT=true.
Örnek
Şöyle yaparız. Burada tüm testler için tek veri tabanı yaratılıyor
import org.h2.jdbcx.JdbcDataSource;

public class H2Test {
  private static Connection connection;

  @BeforeClass
  public static void setUp() throws Exception {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    connection = dataSource.getConnection();

    // create table and insert data for testing
    try (Statement stm = connection.createStatement()) {
     stm.executeUpdate("CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(255))");
     stm.executeUpdate("INSERT INTO test_table (id, name) VALUES (1, 'Test Name')");
    }
  }

  @Test
  public void testDatabase() throws SQLException {
    // perform database test
  }

  @AfterClass
  public static void tearDown() throws Exception {
    // delay closing the database to avoid unnecessary opening and closing
    Thread.sleep(5000); // delay for 5 seconds
    connection.close();
  }
}
Örnek
Şöyle yaparız. Burada her test için yeni bir veri tabanı yaratılıyor
iimport org.h2.jdbcx.JdbcDataSource;

public class H2Test {
  private Connection connection;

  @Before
  public void setUp() throws Exception {
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    dataSource.setUser("sa");
    dataSource.setPassword("");

    connection = dataSource.getConnection();

    // create table and insert data for testing
    try (Statement stm = connection.createStatement()) {
     stm.executeUpdate("CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(255))");
     stm.executeUpdate("INSERT INTO test_table (id, name) VALUES (1, 'Test Name')");
    }
  }

  @Test
  public void testDatabase() throws SQLException {
    // perform database test
  }

  @After
  public void tearDown() throws Exception {
    connection.close();
  }
}
DB_CLOSE_ON_EXIT
Varsayılan değeri false. Tüm testte tek bir veri tabanının kullanılabilmesi içindir. Açıklaması şöyle. Genellikle SpringBoot ile kullanılır. Veri tabanını Spring kapatır
If, for whatever reason, you do configure the connection URL for an embedded database, care should be taken to ensure that the database’s automatic shutdown is disabled. If you’re using H2 you should use DB_CLOSE_ON_EXIT=FALSE to do so. 
Bir başka açıklama şöyle
When DB_CLOSE_ON_EXIT is set to true, the H2 database is closed when the last connection to the database is closed or when the JVM exits. When DB_CLOSE_ON_EXIT is set to false, the H2 database remains open until explicitly closed.
Örnek
Şöyle yaparız
"jdbc:h2:mem:test:sample;DB_CLOSE_ON_EXIT=FALSE;
INIT=RUNSCRIPT FROM 'classpath:schema-generator.sql';"
Örnek
Şöyle yaparız
import org.h2.jdbcx.JdbcDataSource; public class H2Test { private static Connection connection; @BeforeClass public static void setUp() throws Exception { JdbcDataSource dataSource = new JdbcDataSource(); dataSource.setURL("jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=TRUE"); dataSource.setUser("sa"); dataSource.setPassword(""); connection = dataSource.getConnection(); // create table and insert data for testing try (Statement stm= connection.createStatement()) { stm.executeUpdate("CREATE TABLE test_table (id INT PRIMARY KEY, name VARCHAR(255))"); stm.executeUpdate("INSERT INTO test_table (id, name) VALUES (1, 'Test Name')"); } } @Test public void testDatabase() throws SQLException { // perform database test } @AfterClass public static void tearDown() throws Exception { connection.close(); } }
INIT
INIT = XXX şeklinde kullanılır

INIT = CREATE SCHEMA
Örnek
Veri tabanı bellekteyse schema yaratmak için şöyle yaparız. Ancak bu bağlantının kullandığı current schema'yı değiştirmez.
jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS TEST
Açıklaması şöyle
The default schema for new connections is PUBLIC.
Açıklaması şöyle
In H2, the default schema is "public".
Örnek
Veri tabanı bellekte değilse şöyle yaparız
jdbc:h2:test;SCHEMA=SCHEMA_NAME

Hiç yorum yok:

Yorum Gönder