Where are my tables in H2? - java

I deploy my web app to the WildFly 8.02 Final server. I use default out of the box DataSource that comes with the server with jndi name space:
java:jboss/datasources/ExampleDS
and I use default url:
jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
to access H2 from IntelliJ IDEA database client tool or other client applications (I use url connection, because tcp connection via default port doesn't work).
The app is basically an empty WAR with just entity beans and persistence.xml file. I intend to test purely what tables will be created in the underlying data source in accordance to my annotations.
Here is the thing: when I set in persistence.xml:
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
war is succesfully deployed, but when I connect via any client tool to the H2 data source I can see only pre-defined tables after:
SELECT * FROM INFORMATION_SCHEMA.TABLES
I can create tables via client tools and I am able to track their existence by the previous SQL query. So it seems that tables just hadn't been created by the JPA framework.
But when I change persistence.xml to the different schema creation mode:
<property name="javax.persistence.schema-generation.database.action" value="create"/>
Deployment will fail with jdbc exception telling that JPA tries to create already existing tables, but at the same time client tools still do not show any user created tables.
Thx.

Take a look at http://www.h2database.com/html/features.html#in_memory_databases :
To access an in-memory database from another process or from another computer,
you need to start a TCP server in the same process as the in-memory database
was created. The other processes then need to access the database over TCP/IP
or TLS, using a database URL such as: jdbc:h2:tcp://localhost/mem:db1.
Look here for an example:
H2 database in memory mode cannot be accessed by Console

Related

How to add H2 to Wildfly, so I can see the data in the database?

I planned to use ExampleDS (java:jboss/datasources/ExampleDS) for Java development before going to production. It is the default data source in Wildfly, configured using the embedded H2 database for developer convenience. I need to see the data in these tables during development. I am developing in IntelliJ. There is a ”database” tab to the upper right. I have previously added databases here and I could see their content. But I don’t know how to add the ExampleDS database.
I then tried this approach instead:
1) Deploy a prebuilt WAR-file for H2 Console: https://www.cs.hs-rm.de/~knauf/JavaEE6/kuchen/H2Console.war )
2) Open URL http://localhost:8080/H2Console/h2
3) login with username/password = "sa"
I could then access the H2 console. It was however impossible to login with the default JDBC URL
java:jboss/datasources/ExampleDS
I tried with name and password sa, but it always says "wrong password".
So I read the documentation on
https://developers.redhat.com/quickstarts/eap/h2-console/
It says:
To access the test database that the greeter quickstart uses, enter these details:
JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
User Name: sa
Password: sa
So I tried that and I could finally connect. But there seems to be no tables in the database. The documentation says:
Take a look at the data added by the greeter application. Run the following SQL command:
select * from users;
I tried that, but got the message
Table "USERS" not found
The tables that I have created in my JPA app can also not be found. I assume that is because my persistance file points to
java:jboss/datasources/ExampleDS
So I changed it to
jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
Then I got this error when trying to run my JPA app from IntelliJ:
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
Does anypne know how I should proceed with this to actually add my tables to a H2 database and then to see the data in these tables?
I think you don't have some of the concepts clear:
H2 has several options to create a database, using a file as basis or in-memory.
The in-memory is not stored in any place, for this reason if you configure it in your app like this:
jdbc:h2:mem:whatever
The jvm starts a database in their own memory, only available for the vm.
If you want to connect from outside I recommend to use the embedded or server mode.
Ofc an in-memory database will be empty, you have to populate it in start-up.
I used it a lot for testing. It's pretty cool.
Check the H2 cheatsheet:
https://www.h2database.com/html/cheatSheet.html

Database migration from Mysql To SQLServer using hibernate

I want to write a script which will transfer daily data from Mysql to SQL server.
I have decided to do this using hibernate.
There are three databases in mysql called db1, db2 & db3. I want to transfer data from all these databases to SQL server. Table structure on SQL server is identical to MYSQL. Simply we can say this script is to take backup of mysql data on sql server.
Now my question is:
Is there any other simple method to do this?
How can I connect to 3 different databases (db1,db2,db3) of mysql as well as sql server using hibernate?
in hibernate.cfg.xml url property takes one database name only:
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db1</property>
So in this way I need to write 6 hibernate.cfg.xml files to connect to mysql as well as sql server.
One of the constructors for Configuration does take a file path:
public Configuration configure(String resource) throws HibernateException {
standardServiceRegistryBuilder.configure( resource );
// todo : still need to have StandardServiceRegistryBuilder handle the "other cfg.xml" elements.
// currently it just reads the config properties
properties.putAll( standardServiceRegistryBuilder.getSettings() );
return this;
}
So... what you'll need to do is copy your hibernate.cfg.xml file as many times as you need and configure different sessions using the standard way.

Connect to in-memory HSQL database (launched by Spring) from another client

I created a HSQL database with the following Spring configuration:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="resources/sql/schema.sql"/>
<jdbc:script location="resources/sql/test-data.sql"/>
</jdbc:embedded-database>
Everything works fine (for once)! I'd like to connect the same instance of that database from another client (I'm using database support from IntelliJ Ultimate) to alter/monitor the state of some tables for development reasons. Is it possible? How? If not, are there any alternatives/workarounds?
You cannot. HSQL database opened in embedded mode are opened in exclusive mode and can only be used from one single JVM.
There are different alternatives :
the Spring application could implement a protocol to allow administrative supervision of the database
you can use the database in server mode instead of embedded mode
In server mode, there is one dedicated application that opens the database in exclusive mode (the server), and then any client application connects to the server. But you can no longer use <jdbc:embedded-database id="dataSource" type="HSQL"> because it is a true jdbc server.

Configuring web application to connect to database

I have java web application using struts 2 and running on JBoss server. We don't use Spring. This application does not use any database right now. They have moved some hard coded data in the application to database and have asked me to start using the database. I'll have to connect to the database to start with.
I have two conditions that have to be met:
I am told we can't reconfigure the server since it is read only (which means I cannot create DataSource on the server[add *-ds.xml to server] - Does it have any other meaning ?) -
(This leaves me with an option to use DriverManager. Is there a better way ?)
. They have told me to keep the database connection properties file out of the ear file and in the config directory. (Does this mean that I have to place the config file somewhere inside the jboss server? What does this mean ? Will this eventually make me to "reconfigure" the server ?)
How do I place the database connection properties file outside the EAR and still have my DAO layer access the configuration properties ?

Some confusion surrounding JDBC Resources and JDBC Connection pools Glassfish

I am about to make a connection to my database and I am using EJB and JPA. I have recently started doing these technologies so I have some problems understand it all at this point:)
I know that the persistence.xml is needed in order to use JPA. It is the file where I configure how to connect to the database, that I know. However it seems like there are multiple ways of doing it.
What would be the difference (or when should I even use the one alternative over the other?) of defining properties such as username, database, password etc. in the persistence.xml file and in Glassfish? Advantages/disadvantages if any.
Underneath on the image I posted I have JDBC Resources and JDBC Connection Pools. I am a litte confused about the terminology of the two. Why don't we add properties such as username, database, password and so on in the JDBC Resources? Could someone explain the difference between them and what they mean?
JDBC Resources
A JDBC resource (data source) provides applications with a means of
connecting to a database. Typically, the administrator creates a JDBC
resource for each database accessed by the applications deployed in a
domain. (However, more than one JDBC resource can be created for a
database.)
http://download.oracle.com/docs/cd/E19316-01/820-4335/ablih/index.html
I think it strange that we add such properties on the pool but not in the resource, but I probably misunderstand the concepts.
In the "JDBC connection pools" you can create container managed JDBC data sources (with connection pooling capabilities). The data source needs to know about at least the JDBC driver, the JDBC URL, username and password.
In the "JDBC resources" you can bind those container managed JDBC data sources to one or more JNDI names so that they are by JNDI available to the deployed web application.
In the persistence.xml you can specify whether to use a local data source or to use a container managed data source. If you want to use a container managed data source, then you should specify its JNDI name. Using a container managed data source has the advantage that you can share a single data source among multiple web applications. In case of JPA, it has also the advantage that you can make use of JTA (container managed transactions) so that you don't need to call transaction.begin(), commit(), rollback() etc in every method.

Categories