How to run a HSQLDB server in memory-only mode - java

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.

Related

Error when trying to connect to H2 database

Did any of you had this below error when trying to connect to H2?
The release I'm using is 1.4.199, on windows, with TAFJFuctions.
First I run this command:
java -server -cp h2-1.4.199.jar;C:\Users\...\H2\TAFJFunctions.jar org.h2.tools.Server -web -tcp -tcpPort 9092 -tcpAllowOthers -baseDir C:\Users\...\H2\bin
and I've got connected with TCP server running and Web Console server running as well.
Then I've run this command:
SET H2URL=jdbc:h2:tcp://locaLhost/t24db;DB_CLOSE_ON_EXIT=FALSE;MODE=Oracle;TRACE_LEVEL_FILE=0;TRACE_LEVEL_SYSTEM_OUT=0;FILE_LOCK=NO;IFEXISTS=TRUE;CACHE_SIZE=8192;MVCC=TRUE;LOCK_TIMEOUT=60000
And then this command:
java -server -cp h2-1.4.199.jar;C:\Users\...\H2\TAFJFunctions.jar org.h2.tools.Shell -url %H2URL% -driver org.h2.Driver -user t24 -password t24
And here I've got the below error:
"Exception in thread "main" org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/.../H2/bin/t24db" not found, and IFEXISTS=true, so we cant auto-create it [90146-199]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:617)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.engine.Engine.openSession(Engine.java:67)
at org.h2.engine.Engine.openSession(Engine.java:201)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:178)
at org.h2.engine.Engine.createSession(Engine.java:161)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:160)
at java.lang.Thread.run(Unknown Source)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:617)
at org.h2.engine.SessionRemote.done(SessionRemote.java:607)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:143)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:431)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:317)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:169)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:148)
at org.h2.Driver.connect(Driver.java:69)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.h2.tools.Shell.runTool(Shell.java:148)
at org.h2.tools.Shell.main(Shell.java:81)"
Any idea how to get this working?
Thank you,
Carina
With IFEXISTS=TRUE the problem is oblivious, but without this parameter or with attempt to set it to FALSE situation is not going to be changed.
All recent versions of H2 database don't allow remote (including local TCP connections) database creation by default any more due to security reasons. When it is allowed, everyone who can connect to your port can create a new database, get ADMIN privileges in it and therefore get the same access to your system as your JVM and your user account allows.
Unfortunately, H2 1.4.199 throws a confusing error message, it was improved only in 1.4.200 and in this version (when IFEXISTS=TRUE wasn't used) the error message is “Database … not found, either pre-create it or allow remote database creation (not recommended in secure environments)”.
If you use a TCP (or Pg/ODBC) server you need to create your databases in some other way before you try to connect to them with networked protocol.
For example, you can open the JDBC connection (DriverManager.getConnection()) with embedded URL and close it immediately.
You can replace org.h2.tools.Server with org.h2.tools.Console and you'll see another http connection URL (if you start it from the command line), something like http://127.0.0.2:8082?key=12c58e1c5f9ce1ae88a2921f74e7655ed91a80746730cc6bfa8d4bbb464f69ee; with this URL you will be able to create the database from the web interface (only if remove web interface is not enabled).
You can also add the -tool parameter (only to Console, the Server doesn't support it) to get H2 Console icon in the system tray, its context menu has a command to create a new database and this icon can also open the web interface with the same security key. Don't share this key with anyone. It will be different on each restart, however.
You can also use the command-line Shell tool.
https://h2database.com/html/tutorial.html#creating_new_databases
In the worst case you can enable the remove database creation, but it is not really secure even with local connections and it is completely insecure when remote connections are enabled as in your case. You will have a well-known remote security hole in your system, I don't think that it is your intention.
The problem here is that there is no database called t24db already.
In the property spring.datasource.url property which you use to connect to h2, remove the parameter IFEXISTS=TRUE.
This will ensure that the database is created if it does not exist.

Mysql pool connection error with glassfish/payara server

Ping Connection Pool failed for MySQL Pool. Connection could not be allocated because: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Please check the server.log for more details.
In my case this problem got fixed by adding "useSSL" to the pool properties and setting it to false.
This solution works for payara 5 and Mysql 8
Select Mysql 8 as Database Driver Vendor
When you try create the connection pool remove all default propierties and write the properties manually as shown in the below image, something is wrong with default properties becuase the connection can't be reached so to avoid issues please remove them and try add them manually.
Note: don't forget add your mysql connector to
glassfish5\glassfish\lib
glassfish5\glassfish\domains\domain1\lib
If you are trying to connect to MySQL on cloud (Ex. Azure MySQL) make sure the SSL enforcement is off in addition to above and setting useSSL = false.
Azure MySQL
I just want to give solution here as I already read and view many solutions regarding this error but not anyone of them is good for me.
So please follow the step by step process:-
Download and Install Glassfish/Payara Server.
Download and Install latest mysql.
Install it as full and in mysql folder you can find an Connector j folder.
from there copy the executable jar file and paste it into two folders.
glassfish5\glassfish\lib
glassfish5\glassfish\domains\domain1\lib
Now restart the server from netbeans.
Open admin console and then goto JDBC and then select jdbc connection pool
Add particular property :-
Database name
Password
port :- 3306
ServerName :- localhost
Url :-jdbc:mysql://localhost:3306/test
User
6.Enable ping
save
Best of luck

Connect Metabase to Sql Server

I installed Java on my Windows 10 machine and downloaded Metabase.jar, and ran it.
Now I could access metabase at localhost:3000
But I'm unable to setup my SQL Server Database as Data reference.
I'm either getting:
Couldn't connect to the database. Please check the connection details.
or
Timed out after 5000 milliseconds.
Anyone who has done Metabase - SQL Server connection successfully?
Currently, as of Metabase version 0.30.x the answer depends on if you can use a user configured directly on SQL Server or not:
The easier option to setup is with a user configured directly on SQL Server. Then config is just Name: (anything), Host: (full name), Database Name, Username, Password as you would expect. (I believe you should NOT fill in the Windows domain).
On Windows only you can also use native integrated authentication with sqljdbc_auth.dll, just specify integratedSecurity=true connection property (and optionally authenticationScheme=NativeAuthentication). I found if I dropped the x64 sqljdbc_auth.dll in my <java path>\jre\bin folder it was picked up. Metabase config as in the screenshot below, apart from dropping authenticationScheme=JavaKerberos
The cross platform authenticationScheme=JavaKerberos option to use Windows Authentication with a domain user is still doable, but harder. It's these steps:
Create a krb.conf and verify login works with <Java path>\bin\kinit.exe [user]. Here's a template edited to match the Metabase database setup in the screenshot below.
Start the Metabase.jar file with parameters to load the krb.conf. It works for me with java -Djava.security.krb5.conf=c:/<path>/kerberos/krb.conf -jar metabase.jar.
Now configure the database settings in Metabase according to the screenshot below
Moment of truth! ...
Optional: Still not working? Maybe your SQL Server isn't configured for Kerberos? Maybe you need SPN setup? Maybe you need to specify a java.security.auth.login.config (I have not experienced any of that myself, only thing I can offer is you scroll to the bottom of this post and dive in then edit your question with where you got to and what config you have)
Metabase SQL Server setup using Kerberos:
For background, current (August 2018) very long :| sum up with lots of links of how to connect to SQL Server is here in a Metabase discussion topic Note: In v0.30.0 an issue with dynamic port handling (particularly for people using named instances) was fixed.
Related: A neat StackOverflow answer on Kerberos on a Mac
If you are connecting to a "Named Instance" ({SERVER_NAME}/{INSTANCE_NAME}) instead of "Default Instance" ({SERVER_NAME}), your server may be configured to use Dynamic Port.
A quick dirty fix is you can find the current port with below scripts and then use it on Metabase connection parameters. But this port most probably will change when service restart occurs.
SELECT DISTINCT
local_tcp_port
FROM sys.dm_exec_connections
WHERE local_tcp_port IS NOT NULL
or
USE master
GO
Xp_readerrorlog
(look for "Server is listening on ..." line)
A Cleaner way is setting port for your instance by "SQL Server Configuration Manager" - "SQL Server Network Configuration" - "TCP/IP" - Right Click properties - "TCP Dynamic Ports" (There should be 0 and it should be deleted) and "TCP Port" (New default port should be set here). And also "SQL Server Browser" service should run. A more detailed explanation is here

connecting java application with online hosted mysql database

I have build an application in java, application is one and will be used on 3 different systems,And therefore the database of that application must be online to keep all 3 applications with up to date database...
In starting I developed my application based on localhost (wampserver) and used database in "PhpMyAdmin", and hopefully application is fully developed and ready to run.. but the problem is online database connectivity!
I have uploaded my database on a Site in PhpMyAdmin and they provided below information:
and the for connecting my app to this DB is:
String url = "jdbc:mysql://fdb12.biz.nf:3306/";
String dbName = "1738412_wstore";
String driver = "com.mysql.jdbc.Driver";
String userName = "1738412_wstore";
String password = "Password";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
now when I run my application it shows below error:
I dont know what the problem is there, please help me out through this..
I faced the same issue and the following helped me to solve.
http://support.hostgator.com/articles/cpanel/how-to-connect-to-the-mysql-database-remotely
Hopefully, your hosting provider should have same type of cpanel to configure MySQL database for remote connections.
Check for firewall.
Check if mysql is running.
2 things you can try:
Install MySql client locally on your machine and connect like: mysql -h fdb12.biz.nf -u 1738412_wstore -P<password>
This should work before you try anything else.
Make sure you're using the right imports in your code. See [here][1].
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
I think your DB server doesn't allow remote connections.
Try hosting in:
http://www.freesqldatabase.com/freemysqldatabase/
It's free and allow remote DB connections.
Good Luck!!!
Considering the Biz.nf FAQ:
How can I connect to my MySQL/PostgreSQL database? What settings
should I use for my script/software?
MySQL/PosgreSQL database connection can be established with script/software hosted only on your
web hosting account (meaning no remote access is allowed due to
security reasons). The following settings are needed:
So, since biz.nf does not allow remote access, it will be difficult for you to interact with their MYSQL server for your application.
The best solution and the most simple is probably to find a new provider which will allow the remote access. Heroku.com works very well.
If you really want to use the biz.nf services, it will be more difficult.
It's not a script or credential problem.
Maybe you can (probably not), try to login by SSH to configure your server to enable remote access to your MYSQL database server.
From the free plan, you will not have any access to the online SSH tool and you will need to configure your domain for a SSH connection.
In SSH, and it probably will not work considering the FAQ, you could try to simply modify your /etc/mysql/my.cnf file by commenting the bind-address line wich by default only allows local access (127.0.0.1). Then, try to restart your MYSQL service with: a simple service mysql restart to verify if the remote connection works.
If the remote connection still doesn't work after this, the only way I can find for the moment (unless you completely change your hosting provider) would be to create a kind of API hosted directly on the server and your Java program could interacts with your services (In JSON, by example).

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

Categories

Resources