Cannot connect H2 database using Server Mode - java

I use H2 database (file) in my Java app and due to some data appearance problem in IntelliJ, I use file option instead of memory.
Here is my url setting in application.properties:
spring.datasource.url=jdbc:h2:file:~/test-db;
DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;AUTO_SERVER=TRUE
I can connect and see table data via IntelliJ and H2 console by using the url parameter as shown below:
jdbc:h2:file:~/test-db
However, even I connect to the database successfully, the table data is not seen in DBeaver as shown below:
I think I should use tcp option to connect H2 daabase, but I cannot by using the following settings:
spring.datasource.url=jdbc:h2:tcp://localhost/~/test-db
or
jdbc:h2:tcp://localhost/C:/test-db
and get "Unable to open JDBC Connection for DDL execution" error (I use Windows). Any idea?

You're using a file-associated connection to that in-memory DB that can only be used by 1 tool at once.
So when IntelliJ is connected it generates a lock-file to protect it against multi-access.
I would suggest a TCP connection that allows multiple connections - see redundant question here:
H2 database error: Database may be already in use: "Locked by another process"

Related

How to browse h2database

I am using h2 in my spring application on runtime mode
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
I was able to insert and pull data (using postman) but I want to see the database myself and explore the schemes and data
I'm using Intellij
I've installed H2 Client http://www.h2database.com/html/main.html
and browse via sa and blank password (don't understand why the password is blank nor how do I changed it)
where are my tables? am I connected to the right instance?
UPDATE
I see that when I stop my Spring application, I'm still being able to browse to H2 via H2 console, I was expecting it will be offline... I don't get it
If you are using in-memory h2 database then use below JDBC url.
jdbc:h2:mem:testdb
Check the configuration provided at post How to connect H2 console to embedded Spring H2 DB
I found that I could only do this when I used a file database.
url: jdbc:h2:~/nexin;DB_CLOSE_DELAY=-1;MODE=MySQL;MV_STORE=FALSE;MVCC=FALSE
I was using dbVizualizer not Intelij.
As H2 documentation says:
The following connection modes are supported:
Embedded mode (local connections using JDBC)
Server mode (remote connections using JDBC or ODBC over TCP/IP)
Mixed mode (local and remote connections at the same time)
To be able to simply connect to a database from two separate applications, the connection mode should be Server Mode or Mixed Mode (read h2 documentation for further information). The mode you are using is determined by the connection url you use to connect to it and in your case the connection url is jdbc:h2:~/test which means that you're starting H2 in an Embedded Mode. This means that the database server will be started from within your application and will be accessible only to that single JVM.
TLDR
You can simply change the mode to Mixed mode using Automatic Mixed-Mode feature. To do so, just change your jdbc url (in both applications - your app and the client app) to: jdbc:h2:~/databasefile;AUTO_SERVER=TRUE.
This ~/databasefile is a location of where actual data will be stored - again, it is important to access the same file for both connections/applications.
If you're using Spring Boot with default configuration, to change the jdbc url you just have to add following property:
spring.datasource.url=jdbc:h2:~/databasefile;AUTO_SERVER=TRUE

Java, MySql -- connecting from another computer

I created a java program that uses MySql. Now I want to give the app to someone else, and I'm using the URL as: jdbc:mysql://ipAddress:3306/tableName, but it doesn't work, showing the error of connection failure.
What should I do?
It's a troubleshooting stuff, do the following.
Check the Firewall and Port access on MySQL Server computer.
Check the MySQL Service and the DB User is doing good.
Follow the following thread
How to allow remote connection to mysql

H2 Database Auto Server mode : Accessing through web console remotely

I am fairly new to H2 Database. As a part of a PoC, I am using H2 database(version : 1.4.187) for mocking the MS SQL Server DB. I have one application, say app1 which generates the data and save into H2. Another application, app2, needs to read from the H2 database and process the data it reads. I am trying to use Auto Server mode so that even if one of the application is down, other one is able to read/write to/from the database.
After reading multiple examples, i found how to build the h2 url and shown as below:
jdbc:h2:~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
Enabled the tcp and remote access as Below:
org.h2.tools.Server.createTcpServer("-tcpAllowOthers","-webAllowOthers").start()
With this, I am able to write to the database. Now, I want to read the data using the h2-web-console application. I am able to do that from my local machine. However, I am not able to understand how I can connect to this database remotely from another machine.
My plant is to run these two apps in an ubuntu machine and I can monitor the data using the web console from my machine. Is it not possible with this approach?
How can I solve this ?
Or do I need to use server mode and explicitly start the h2 server? Any help would be appreciated.
By default, remote connections are disabled for H2 database for protection. To enable remote access to the TCP server, you need to start the TCP server using the option -tcpAllowOthers or the other flags -webAllowOthers, -pgAllowOthers
.
To start both the Web Console server (the H2 Console tool) and the TCP server with remote connections enabled, you will have to use something like below
java -jar /path/to/h2.jar -web -webAllowOthers -tcp -tcpAllowOthers -browser
More information can be found in the docs here and console settings can be configured from here
Not entirely sure but looking at the documentation and other questions answered previously regarding the same topic the url should be something like this:
jdbc:h2:tcp://<host>:<port>/~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
It seems that the host may not be localhost and the database may not be in memory
Is there a need for the H2 web console?
You can use a different SQL tool using the TCP server you have already started. I use SQuirreL SQL Client (http://squirrel-sql.sourceforge.net/) to connect to different databases.
If you need a web interface you could use Adminer (https://www.adminer.org/) which can connect to different database vendors, including MS SQL, which happens to be mode you're running H2. There is an Adminer Debian package that should work for Ubuntu.

How to create a batch file for connecting to Java DB Network Server

I have made a Netbeans application that is reliant on the DB Network Server in order to retrieve data. In Netbeans the code works fine and runs well. Outside of Netbeans everything but the database information is working. I have made a batch file for connecting to the localhost server that seems to connect on the port I assigned: 1527. Even after connecting to the localhost, it won't display the database information.
My code in the batch file:
PATH C:\Program Files\Java\jdk1.8.0_25\db\bin;%PATH%
startNetworkServer
When I run this I get the result:
Security manager installed using the Basic server security policy.
Apache Derby Network Server - 10.10.1.8 - (1557168) started and ready to accept connections on port 1527connect
Is there more code that I need to add in order to connect to the actual database itself from the server? Or is this not the right way to do this at all? I have tried using the Embedded DB but that didn't work and only caused more problems. I would prefer greatly to stay away from it and stick to the Network DB.

HSQLDB databases in SQL clients

I have a memory HSQLDB database with this connection URL:
jdbc:hsqldb:mem:test_database
It runs fine with my application but I need to configure this database in a SQL Client.
I can't because every clients complain that no host was found or there's no database.
I'm not sure if I'm filling all the information correctly in "host" and "database" fields or if it is a HSQLDB memory restriction.
Has anyone got the same error?? Thanks a lot.
With :mem: you define a database which is only accessible within the running java vm. This database resides in memory and cannot be accessed externally via host/port jdbc access.
Please read:
Running and Using Hsqldb
Advanced Topics
You can use the Database Manager provided by HSQLDB, just run in console
java -cp hsqldb.jar org.hsqldb.util.DatabaseManagerSwing
and connect to the jdbc:hsqldb:mem:test_database

Categories