How to access a hsqldb DB with JDBC tools? - java

I have a hsqldb file database.
Therefore a folder contains:
hsqldb.script
hsqldb.properties
hsqldb.log
hsqldb.lck
hsqldb.tmp
When I open the script, I can see that SQL tables and INSERTs are present.
How can I use JDBC tools like squirrel to inspect the tables like a real database?
I tried connecting using jdbc:hsqldb:file:C:\path\to\my\data\hsqldb while the application is running that uses this DB.
I could connect, but all I could see are the initial INFORMATION_SCHEMA tables.
What am I missing here? Where is the data that I can see clearly in the .script, but not with an JDBC tool?

The .lck and .tmp files indicate that the database was not shutdown prior to last connection close. Use the SHUTDOWN command to close the database.

Related

h2 database: Unsupported database file version or invalid file header in file

As the title reads, I am trying to open a .h2.db file using DataGrip on MacOS. However, I keep getting the error
Unsupported database file version or invalid file header in file
With the error code
[90048-192]
How do I check the actual database file version and how to get the correct driver files(?) ?
For me, the problem was with the JDBC URL in the h2 login screen.
It was a random URL and after I changed it to the spring.datasource.url value I had set in my application.properties, it started working.
I had this error with a persistent TCP H2 database.
After updating the H2 version, the old persisted database file still existed.
I had to delete the old database file to get rid of the error.
My Error Code was [90048-214].
Try to connect with a database tool like SQuirreL to your database and execute SELECT H2VERSION() FROM DUAL or select value from information_schema.settings where name =
'info.VERSION'; to get the actual version.
Maybe this link on how to upgrade a h2 database can also be helpful: How to check if a h2 database needs to be upgraded?
One more thing: your database could be corrupted. You could try to recover the data by using the recovery tool http://www.h2database.com/html/advanced.html#using_recover_tool or to check the database on corruption How to check h2 database health and corruption

How to get all the tables which I created using hsqldb runserver in standlone mode

I created the jdbc connection successfully for hsqldb in stanlone mode.I found where the new database is created.Using the path of newly created database in runserver ,I created some tables.But when I tried to get those table using jdbc connection,I could not get those table.
connection url is:"jdbc:hsqldb:file:databasename;ifexist=true","SA",""
for getting tables I m using the following query.
"select table_name from information_schema.system_tables where table_type='table'";
I am using hsqldb 2.2.7.
You seem to be trying to access the database simultaneously via Server and in-process. This is not possible. After each type of access, you need to shutdown the database before you connect to it via the alternative type of access. Therefore shutdown the Server once you have created the tables, then connect to the database in-process.
It is easier to access the database via Server only, and avoid such problems. Please also upgrade to version 2.2.8 to avoid a bug which may affect a database that has not been shutdown properly.

H2 database error: Database may be already in use: "Locked by another process"

I am trying to use the H2 database from a Java application.
I created the database and its tables through the H2 Console and then I try to connect from Java using
Connection con = DriverManager.getConnection("jdbc:h2:~/dbname", "username", "password");
However I receive the following error:
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-161]
I tried to delete the dbname.lock.db file but it is automatically re-created.
How can I unlock the database to use it from my Java program?
H2 is still running (I can guarantee it). You need to use a TCP connection for multiple users such as ->
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/C:\Database\Data\production;"/>
OR
DriverManager.getConnection("jdbc:h2:tcp://localhost/server~/dbname","username","password");
It also means you need to start the server in TCP mode. Honesetly, it is pretty straight forward in the documentation.
Force kill the process (javaw.exe for Windows), and make sure that any application that might have started it is shut down. You have an active lock.
I had the same problem.
in Intellj, when i want to use h2 database when my program was running i got the same error.
For solve this problem i changed the connection url from
spring.datasource.url=jdbc:h2:file:~/ipinbarbot
to:
spring.datasource.url=jdbc:h2:~/ipinbarbot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
And then my problem gone away. now i can connect to "ipinbarbot" database when my program is.
If you use Hibernate, also don't forget to have:
spring.jpa.hibernate.ddl-auto = update
goodluck
I'm using h2db with a test T24 tafj application, I had the same problem but I managed to resolve it by identifying the application that is running h2 (launched when I attempted to setup a database connection).
ps aux|grep java
will give output as:
sysadmin 22755 3.2 0.1 5189724 64008 pts/3 Sl 08:28 0:00 /usr/java/default/bin/java -server -Xmx2048M -XX:MaxPermSize=256M -cp h2-1.3.175.jar:/r14tafj/TAFJ/dbscripts/h2/TAFJFunctions.jar org.h2.tools.Server -tcp -tcpAllowOthers -baseDir /r14tafj/t24/data
now kill this with its process id:
kill -9 22755
and at last remove the lock file:
rm -f dbname.lock.db
I got clue from Saman Salehi above.
My usecase:
Preparing REST application for client-side load balancing(running two JVM instances of REST). Here my MVC application will call this REST application that has ActiveMQ backend for DATA.
I had the problem when I ran two instances of REST application in eclipse and trying to run both instances at the same time with the following configuration
spring.datasource.url=jdbc:h2:file:./Database;
spring.jpa.properties.hibernate.hbm2ddl.auto=update
After adding DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
spring.datasource.url=jdbc:h2:file:./Database;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Both instances are running and showing in Eureka dasboard.
Don't close the database when the VM exits : jdbc:h2:;DB_CLOSE_ON_EXIT=FALSE
Multiple processes can access the same database without having to start the server manually ;AUTO_SERVER=TRUE
Further reading:
http://www.h2database.com/html/features.html
You can also visit the "Preferences" tab from the H2 Console and shutdown all active sessions by pressing the shutdown button.
Simple step: Go to the task manager and kill the java process
then start your apllication
You can also delete file of the h2 file database and problem will disappear.
jdbc:h2:~/dbname means that file h2 database with name db name will be created in the user home directory(~/ means user home directory, I hope you work on Linux).
In my local machine its present in: /home/jack/dbname.mv.db I don't know why file has a name dbname.mv.db instead a dbname.
May be its a h2 default settings.
I remove this file:
rm ~/dbname.mv.db
OR:
cd ~/
rm dbname.mv.db
Database dbname will be removed with all data. After new data base init all will be ok.
If you are running same app into multiple ports where app uses single database (h2), then add AUTO_SERVER=TRUE in the url as follows:
jdbc:h2:file:C:/simple-commerce/price;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;AUTO_SERVER=TRUE
I ran into similar problems running with ORMLite from a web application. I initially got stuck on the syntax to use server mode in the url. The answers above helped with that. Then I had the similar user/password error which was easier to figure out. I did not have to shut anything down or erase any files. The following code worked:
protected ConnectionSource getConnectionSource() throws SQLException {
String databaseUrl = "jdbc:h2:tcp://localhost/~/test";
return new JdbcConnectionSource(databaseUrl,"sa","sa");
}
To use H2 in server mode on wildfly, I Modifed connection-url in standalone.xml
<datasource jndi-name="java:jboss/datasources/ExampleDS" pool- name="ExampleDS" enabled="true" use-java-context="true">
<connection-url>jdbc:h2:tcp://localhost/~/test</connection-url>
…
</datasource>
Identify the H2 process id and kill it. For mac
ps -ef|grep h2
Then get the process id and kill it.
kill -9 PID
According H2 Database Tutorial you can run the H2 Database in three different modes:
Server mode:
jdbc:h2:tcp://localhost/~/test
When using H2 db in server mode (also known as client/server mode) all data is transferred over TCP/IP. Before application can use H2 Database in server mode, you need to start the H2 DB within the same or another machine.
Embedded mode:
jdbc:h2:~/test
H2 db in embedded mode will be faster but the downside of it is that no other process can access the Database. In the above connection string, the Data will be saved into the ‘test’ folder, under the user’s home directory.
Mixed mode:
The mixed mode combines some features of the embedded and the server mode. The first application connecting to the H2 db does that in embedded mode, but at the same time it starts a server so that other applications can concurrently access the same data, even from different processes.
jdbc:h2:/data/test;AUTO_SERVER=TRUE
When using automatic mixed mode, you can share the JDBC URL for all applications using the DB. By default the server uses any free TCP port. The port can be set manually using AUTO_SERVER_PORT=9090.
Ran into a similar issue the solution for me was to run fuser -k 'filename.db' on the file that had a lock associated with it.
Hope this helps!
I was facing this issue in eclipse . What I did was, killed the running java process from the task manager.
It worked for me.
In your application.properties file > edit the datasource into:
spring.datasource.url=jdbc:h2:file:C:/temp/test;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
Happy coding!
answer for this question => Exception in thread "main" org.h2.jdbc.JdbcSQLException: Database may be already in use: "Locked by another process". Possible solutions: close all other connection(s); use the server mode [90020-161]
close all tab from your browser where open h2 database also Exit h2 engine from your pc
For InteliJ: right lick on your database in the database view and choose "Disconnect".
I tried to delete the dbname.lock.db file but it is automatically re-created.
How can I unlock the database to use it from my Java program?
Just add FILE_LOCK=NO;. FILE_LOCK=NO doesn't make dbname.lock.db.
spring.datasource.url=jdbc:h2:file:./testdb/h2;DB_CLOSE_ON_EXIT=false;FILE_LOCK=NO;
The detail for FILE_LOCK reference this.
Using the method NO forces the database to not create a lock file at all

does postgresql jdbc driver has \copy?

as stated,
does java jdbc driver for postgresql has client side \copy?
I wanted to do batch inserts into a table in the database on a remote machine with data from my text file on another machine.
Since the 8.4 driver there is support for the COPY command, through the CopyManager:
http://jdbc.postgresql.org/documentation/publicapi/org/postgresql/copy/CopyManager.html
add ip of your system from where you used to update database in pg_hba.conf file
connect usinyg url
jdbc:postgresql://host:port/database
execute query

How to run a HSQLDB server in memory-only mode

In the documentation of the HSQLDB is a command line statement to start a HSQLDB server (HSQLDB Doc). But there is this "file:mydb" property, so I assume its not in memory-only mode.
How do I run a memory-only HSQLDB server?
I ran the following but get no clue.
java -cp ../lib/hsqldb.jar org.hsqldb.Server -?
It took around 2 days for me to figure out on how to start a server in-memory and then access from outside. Hope this will save someone's time.
Server server = new Server();
server.setDatabaseName(0, "mainDb");
server.setDatabasePath(0, "mem:mainDb");
server.setDatabaseName(1, "standbyDb");
server.setDatabasePath(1, "mem:standbyDb");
server.setPort(9001); // this is the default port
server.start();
When you have to access the in-memory database for any CRUD, here is what you need to do :-
String url="jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb";
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection conn = DriverManager.getConnection(url, "SA", "");
where 192.168.5.1 is the server ip where HSQL is running. To connect to the standbyDb, replace mainDb with standbyDb in the first line. Once you get the connection, you can perform all database related operations.
To connect to the server from remote using DatabaseManagerSwing, here is what you need to do.
Download hsqldb-x.x.x jar and copy it to a folder (x.x.x is the version)
open a terminal or command prompt and cd to the folder and run
java -cp hsqldb-x.x.x.jar org.hsqldb.util.DatabaseManagerSwing
Select "HSQL Database Engine Server" from the Type drop down and give "jdbc:hsqldb:hsql://192.168.5.1:9001/mainDb" as the URL. This will connect you to the remote HSQL in-memory Server instance.
Happy Coding !!
DbManagerSwing UI
use java -cp .\hsqldb-1.8.0.10.jar org.hsqldb.Server -database.0 mem:aname
In memory mode is specified by the connection url - so if you want, you can just have a server.properties file in the same directory, and set the connection url to use the mem protocol - or if you are using hsqldb in another application that allows you to specify the connection url such as jdbc, specify jdbc:hsqldb:mem:aname.
I believe the file is used to load up the db into memory, and then persist when Server stops. I don't think the file is accessed while you're running.
It's been awhile since I've used HSQLDB (or H2), but I'm pretty sure that's how it works.

Categories

Resources