H2 - Multiple applications access same H2 database - java

I am using embedded database H2 in 2 web applications say WebApp1 and WebApp2. I run WebApp1 and execute some query to access H2 database. Mean while I run WebApp2, but it throws exception that H2 is currently been used by another process
My need is, I should be able to use H2 database by WebApp1 and WebApp2 at the same time. I analysed the below link but it tells about multiple connections but not about multiple applications.
http://www.h2database.com/html/features.html#multiple_connections

You can use the embedded mode for WebApp1 and the server mode for WebApp2: in order to get this trick, both urls must be like "jdbc:h2:mydb;AUTO_SERVER=TRUE"
More info here: http://h2database.com/html/features.html#auto_mixed_mode

Related

MySQL "No database selected" over jdbc

We have a Java application (basically integration tests) that uses Hibernate (which uses Jdbc) to read/write data to the MySQL Database. Hibernate objects like sessions or transactions are created and configured via our own code (no Spring or other wrappers are being used). The issue is that periodically (multiple times during tests execution) we observe a "No database selected" exception. Database URL that we use for DataSource configuration already contains database name in it:
jdbc:mysql://localhost:3306/test?useSSL=false&createDatabaseIfNotExist=false&cacheServerConfiguration=true&cacheResultSetMetadata=true&useLocalSessionState=true&rewriteBatchedStatements=true&tcpNoDelay=true&tcpTrafficClass=16&alwaysSendSetIsolation=false&tcpSndBuf=1048576&tcpRcvBuf=1048576&characterEncoding=utf8&allowPublicKeyRetrieval=true
I tried to catch the Exception and test the connection's selected database by running select database() and it actually reports that the value is null on the database side.
Even more strange thing is that next queries on the same connection are executed against the normal database (so it somehow self-heals).
Does anybody know why can MySQL connections "lose" and then "restore" selected database?
Or maybe there is a way to trace the problem down. Will be grateful for any help or thought that you can provide
Versions:
Java 1.8.0_292
Mysql 5.6.31
Hibernate 5.4.2
JDBC mysql-connector-java 8.0.22

How can I turn off logging in HSQL DB with URL parameter?

I am using java-ee application running on wicket.
The problem is, that the connection to the HyperSQL database with JDBC creates a huge log file *.log. Example of my URL:
jdbc:hsqldb:file:C:\database\text;hsqldb.sqllog=0;hsqldb.applog=0
I need to turn off the logging with some jdbc url parameter. I have tried these:
hsqldb.sqllog=0;hsqldb.applog=0;
That is not working, so I have tried this combination:
hsqldb.log_data=false;hsqldb.reconfig_logging=false
But with these parameters, when I stop the app, the data disappears.
The text.log file is the record of transactions performed on your database. If you disable it with hsqldb.log_data=false the transactions are not persisted to disk. The name is derived from the file name on your database URL.
The hsqldb.sqllog and hsqldb.applog settings are for diagnostics and default to 0.
The hsqldb.reconfig_logging=false is also for diagnostics and works together with hsqldb.applog setting. These settings are discussed in the Guide: http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_monitoring_operation
So if you want your data to survive when your app shuts down, you should not disable the default log. See http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_cache_persistence on how to reduce the size of the text.log file.

Stop H2 database programmatically

Consider we have a H2 database which is started from a web-application under Tomcat using Hibernate. In other words it is an embedded H2 database into the application.
The question: is it possible to programmatically stop this H2 server from this application and then start it again?
P.S. Server.createTcpServer(args).start(); or Server.shutdown(...) is not the way, because it is in the embedded mode.
In this particular case such an approach will be a workable solution:
To stop H2 database just use SHUTDOWN sql command:
session.createSQLQuery("SHUTDOWN").executeUpdate();
To restart H2 you don't need to do anything: the Tomcat's connection pool will do it automatically.

running my JDBC application in other systems

I created a JDBC application using mysql5.5.14 .
I want to run it on other systems without mysql5.5.14.(but having java)
Is thee any way i can install(and configure) mysql5.5.14 on other systems via my application.
I'm not entirely sure what you're trying to do, but if you mean "configure" as in "create the same database tables and views", you can use something like Liquibase.
If you want to keep using JDBC but with another database system at the backend, you can just pass the JDBC driver's class name to the program through a configuration file and load the driver using
Class.forName(jdbcDriver)
when you start up your application.
HTH
Raku

Best way to access a sqlite database file in a web service

First question from me on stack overflow.
I have created a java web application containing a web service using netbeans (I hope a web application were the correct choice). I use the web application as is with no extra frameworks. This web service use a sqlite JDBC driver for accessing a sqlite database file.
My problem is that the file path end up incorrect when I try to form the JDBC connection string. Also, the working directory is different when deploying and when running JUnit tests. I read somewhere about including the file as a resource, but examples of this were nowhere to be seen.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
I don't know much about web services, I just need it to work, so please, help me with the technicalities.
Update
To put this a litle bit in context, some "println" code gives this:
Printing the work directory from a simple JUnit test gives
C:\MinaFiler\Work\SOA\BusTimetableWS
Invoking a similar web servic method returns
C:\Program Files\sges-v3\glassfish\domains\domain1
The connection string is formed from prepending "jdbc:sqlite:" to the path which at the moment is absolute:
C:\MinaFiler\Work\SOA\BusTimetableWS\src\java\miun\bustimetable\database\sqlit\BusTimetableWS.db
However, this fails because my tests throws exceptions stating database tables doesn't exist although they really do, I can see them with sqlite3.exe .
One way would be to use a config file that you can read and fetch your connection string from there.
I'm sure the framework you are using has some kind of standard way of saving configurations.
Another option would be to place the db in a known relative path from your main execution files. Then when executed fetch your current directory, and look for the db from that path.
In any case, what is the best way to open the sqlite database, both when the web service is deployed and when I test it "locally"?
The web service should use a DataSource to retrieve a connection from a connection pool configured at the application server level. In your unit test, use whatever you want (a standalone connection pool, a direct JDBC connection).
But in both cases, why don't you use an absolute path to the database file in your jdbc url? From How to Specify Database Files:
jdbc:sqlite:C:/work/mydatabase.db
The working directory wouldn't matter if you do so.

Categories

Resources