Showing posts with label h2. Show all posts
Showing posts with label h2. Show all posts

Saturday, June 2, 2018

Spring Boot and H2

https://dzone.com/articles/spring-boot-and-spring-jdbc-with-h2

Example code is here:

https://github.com/in28minutes/spring-boot-examples/tree/master/spring-boot-2-jdbc-with-h2

you must add spring.h2.console.path=/h2 in the application.properties file

in a nutshell:

in pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>


in application.properties

# Datasource
#spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.url=jdbc:h2:file:~/mydb
spring.datasource.username=mydb
spring.datasource.password=mydb
spring.datasource.driver-class-name=org.h2.Driver

# H2
# Enabling H2 Console
spring.h2.console.path=/h2
spring.h2.console.enabled=true
#Turn Statistics on
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.stat=debug
# Show all queries
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace


#spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=validate


To implement a repository:


import org.springframework.data.repository.CrudRepository;

public interface QuarantineRepository extends CrudRepository<Quarantine, Long> {
List findBygav(String gav);

}


To use the repository in a @javax.inject.Named bean:

import javax.inject.Inject;
...

@Inject
QuarantineRepository quarantineRepository ;


where Quarantine is like this:


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Quarantine {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String gav;

etc etc

}



Friday, April 20, 2018

h2 remote TCP client

telnet ip port

Remote connections to this server are not allowed, see -tcpAllowOthers
org.h2.jdbc.JdbcSQLException: Remote connections to this server are not allowed, see -tcpAllowOthers [90117-197]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:89)
at java.lang.Thread.run(Thread.java:748)
Connection closed by foreign host.


You fix the issue by running h2 server as:

java -cp "/opt/h2/bin/h2-1.4.197.jar:$H2DRIVERS:$CLASSPATH" -Dh2.consoleTimeout=500000 org.h2.tools.Console -web -webAllowOthers -tcp -tcpAllowOthers


on the client side, in wildfly, you have

<datasource jndi-name="java:jboss/datasources/DOICHDS" pool-name="DOICHDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:tcp://yourserverip:9092/~/PIPPO;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE</connection-url>
<driver>h2</driver>
<security>
<user-name>PIPPO</user-name>
<password>PIPPO</password>
</security>
</datasource>

See https://stackoverflow.com/a/9838282/651288



Saturday, April 7, 2018

H2 autogenerating unique IDs from Sequence

if you have annotated the ID with

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Entity;

@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "ID"), name = "VOCABULARY")
public class Vocabulary implements Serializable {

@Id
@GeneratedValue
private Long id;

and declared the ID as
CREATE TABLE "VOCABULARY" (
    "ID" NUMBER(10,0) NOT NULL
...
)
CREATE UNIQUE INDEX "VOCABULARY_UNIQ_ID" ON "VOCABULARY" ("ID") ;
ALTER TABLE "VOCABULARY" ADD PRIMARY KEY ("ID");

this is not enough... you will get this when you try to insert a new record:

org.h2.jdbc.JdbcSQLException: Sequence "HIBERNATE_SEQUENCE" not found; SQL statement:
call next value for hibernate_sequence [90036-197]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
        at org.h2.message.DbException.get(DbException.java:179)
        at org.h2.message.DbException.get(DbException.java:155)
        at org.h2.command.Parser.readSequence(Parser.java:5970)
        at org.h2.command.Parser.readTerm(Parser.java:3131)
        at org.h2.command.Parser.readFactor(Parser.java:2587)
        at org.h2.command.Parser.readSum(Parser.java:2574)
        at org.h2.command.Parser.readConcat(Parser.java:2544)
        at org.h2.command.Parser.readCondition(Parser.java:2370)
        at org.h2.command.Parser.readAnd(Parser.java:2342)
        at org.h2.command.Parser.readExpression(Parser.java:2334)
        at org.h2.command.Parser.parseCall(Parser.java:4854)
        at org.h2.command.Parser.parsePrepared(Parser.java:382)
        at org.h2.command.Parser.parse(Parser.java:335)
        at org.h2.command.Parser.parse(Parser.java:307)
        at org.h2.command.Parser.prepareCommand(Parser.java:278)
        at org.h2.engine.Session.prepareLocal(Session.java:611)
        at org.h2.server.TcpServerThread.process(TcpServerThread.java:271)
        at org.h2.server.TcpServerThread.run(TcpServerThread.java:165)
        at java.lang.Thread.run(Thread.java:748)

        at org.h2.engine.SessionRemote.done(SessionRemote.java:624)
        at org.h2.command.CommandRemote.prepare(CommandRemote.java:68)
        at org.h2.command.CommandRemote.(CommandRemote.java:45)
        at org.h2.engine.SessionRemote.prepareCommand(SessionRemote.java:494)
        at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1203)
        at org.h2.jdbc.JdbcPreparedStatement.(JdbcPreparedStatement.java:73)
        at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:676)
        at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.doPrepareStatement(BaseWrapperManagedConnection.java:757)
        at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.prepareStatement(BaseWrapperManagedConnection.java:743)
        at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareStatement(WrappedConnection.java:454)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172)



Solution is:

CREATE SEQUENCE VOCABULARY_SEQUENCE_ID START WITH (select max(ID) + 1 from VOCABULARY);

(this allows me to prepopulate the VOCABULARY with static values, and initialize the sequence accordingly)

and in Java

@Id
@SequenceGenerator(name= "VOCABULARY_SEQUENCE", sequenceName = "VOCABULARY_SEQUENCE_ID", initialValue=1, allocationSize = 1)
@GeneratedValue(strategy=GenerationType.AUTO, generator="VOCABULARY_SEQUENCE")
private Long id;



Why in 2018 such common things have to be still so complicated, no clue, something is really going wrong in the Java world... maybe using Spring it's only a matter of 1 annotation... no clue...





Sunday, March 25, 2018

h2 remote

./h2.sh -tcpAllowOthers -webAllowOthers


Web Console server running at http://localhost:8082 (others can connect)
Failed to start a browser to open the URL http://localhost:8082: Browser detection failed and system property h2.browser not set
TCP server running at tcp://localhost:9092 (others can connect)
PG server running at pg://localhost:5435 (only local connections)


At this point you can connect remotely to the console (even if it says "localhost") and you can specify a remote tcp url in your datasource:

jdbc:h2:tcp://YOURIP:9092/~/YOURDB;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE

When using h2, don't forget to use

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

in your persistence.xml
(see http://www.mastertheboss.com/jboss-server/jboss-datasource/h2-database-tutorial )


Sunday, November 5, 2017

H2 Database Tutorial

presentation here (English is iffy but content is good until half way, the rest forget it)




Download it from here http://www.h2database.com/html/download.html

go to d:\apps\H2\bin and run h2.bat

this will open the browser at http://192.168.56.1:8082/login.jsp?jsessionid=884e38fb4c3da545d97e2fc9be776bd8


the JAR is D:\apps\H2\bin\h2-1.4.196.jar


How to use H2 as embedded DB




Interesting project demonstrating embedded H2 DB https://www.mkyong.com/spring/spring-embedded-database-examples/

but also this one https://github.com/Pscheidl/cdi-events-playground demonstrating CDI, JSF and H2