Java webapplication deploymant with relational database - java

We can deploy a web application in java container using a war. But how schema creation is handled with deployment?
Is there a way to include schema and tables creation statements with war? Or do we have to create schema with required tables manually?
How database setup is normally handled with war deployment in a java container like tomcat?

Usually during development any action with DB is performed by your ORM(ex. Hibernate). But in production mode it's not applicable and you should use some additional tools.
For example in our project we are using Flyway.

Related

ElasticSearch Schema Migrations

When we write an application, we also write migrations for database schema and data, we use liquibase for that.
For example I am deploying an application locally as developer on my machine, application uses elasticsearch, but before adding some data I need to execute some query/mappings.
So, I want to add that query/mappings to some files, so, when another developer will deploy application on his machine, that query will be executed automatically.
What tools/library are using for migrations ElasticSearch schema?
I created a long time ago a project I'm still maintaining which creates an index with all settings and mappings if it does not exist at startup.
elasticsearch-beyonder is available at https://github.com/dadoonet/elasticsearch-beyonder.
For now, it works only with a TransportClient but I'm planning to add support for the Java REST Client as well.

How to manage a Spring-MVC web application in Eclipse with same workspace for different database testing

I am working on a Spring MVC project with running Tomcat server and using Eclipse STS IDE.
I need to test my project's functionality with different databases (SQL, oracle and postgreSQL database).
To test same functionality in all databases, every time I am changing applicationProperies.properties file to change database configuration and restarting the sever. Is there any way where I can test my application parallely with all databases?

Glassfish 3 with JPA configuration

I have use JPA with Hibernate in a standalone application but now I want to try with with an application server. I know GlassFish provides EclipseLink implementation for JPA but I have a few questions.
Do I need to specify in persistence.xml EclipseLink as a provider for my persistence-unit?
Does persistence.xml look the same as if it the application would not be deployed? If it does not look the same how does it look?
Do I need to specifically download the implementation jars for EclipseLink and build with them or does the container handles this after my application is deployed?
How do I specify the jdbc driver in persistence.xml?
Does my application need to be deployed as a .ear?
You don't need to specify the persistence provider, by default the one contained in your application server will be used (if it has at least the Web profile, of course, otherwise servers such as Tomcat won't provide you EclipseLink).
Yes, it will have the same look (in both applications you are just using JPA the same way).
For your code to compile, you will only need to have persistence-api.jar in your classpath (if you use Maven, set the scope to "provided"). Then the server will automatically provide its implementation jars.
You could use a persistence unit like described in this page ("typical configuration in a Java SE environment"). But I would rather suggest you use a <jta-data-source> instead, that refers to a datasource provided by GlassFish.
As far as I can tell, it can also be a WAR file, I didn't have any problem deploying it (webapp as a Maven WAR module + beans in a JAR module).

Glassfish: modify deployment descriptors for EAR during deployment

After several days of searching, trying and head-banging, I post this question to SO although it seems to be answered already.
Here is the scenario:
I have an EAR application containing (for the moment) one WAR and one EJB module. The EJB module uses JPA (persistence.xml) and some Stateless Session Beans are exposed via Web Services. The web services use Basic authentication with a jdbc realm. The web module uses form authentication with the same realm.
The requirement:
I need to be able to deploy this application either on different servers (dev/test/prod) or on the same server (or cluster) with different deployment descriptors. The deployment settings that need to be different in each application instance are:
The jta-data-source in persistence.xml
The realm-name in web.xml
The javax.faces.PROJECT_STAGE in web.xml
The webservice-endpoint\endpoint-address-uri and login-config\realm in glassfish-ejb-jar.xml
The context-root in application.xml (i could move it to web.xml if it made any difference, see below)
The realm in glassfish-application.xml
During my research, I managed the following:
I can override the javax.faces.PROJECT_STAGE using asadmin set-web-context-param
I can override all settings in glassfish-ejb-jar.xml using a deployment plan during asadmin deploy
The same applies for glassfish-application.xml
I can probably override context-root during asadmin deploy (I don't know how would this work with more than one web modules in the EAR)
So far so good. This leaves me with the following problems:
How can I easily modify the the realm-name in web.xml?
How can I easily modify the jta-data-source in persistence.xml?
By easily I mean during deployment or using something similar to a deployment plan jar. Maintaining multiple copies of ejb.jar or war just with a modified .xml file is not an option.
Just to be clear, the need is to have different databases (either in different stages of development or for different customers) using the same application. The application uses one persistence-unit but it needs to point to different databases (hence the jta-data-source). The realm is a jdbc realm (on the same database) that also needs to be different per application instance.
Any help or pointer would be greatly appreciated.
Have you thought about preparing templates for the deployment descriptors, and populating them with value from property file during build? If you are using ant, you can use the expandproperties filter.
You can do all those things with a deployment plan jar.
It looks like the content of the deployment plan jar is pushed into archive/directory tree of the application BEFORE any of the heavy lifting associated with deployment happens.
See
http://java.net/projects/glassfish/sources/svn/content/trunk/main/appserver/deployment/javaee-core/src/main/java/org/glassfish/javaee/core/deployment/DolProvider.java
and
http://java.net/projects/glassfish/sources/svn/content/trunk/main/appserver/deployment/dol/src/main/java/com/sun/enterprise/deployment/archivist/Archivist.java

Spring MVC Web App with Default user

I am developing a web app using Spring MVC and Hibernate that I hope to package up as a WAR file and distribute for users to deploy where needed.
The application will require the database connection details before deploying, can anyone advise on the best practice of how to do this? currently my database.properties file is packaged as part of the WAR file, is there an easier way than asking the user to manually edit this file inside the WAR file or in the source and then expect them to build the war file themselves?
I would also like an easy way for users to create a single admin user on first deploy easily - is it best to just provide a sql script to create that or is there a way to include the functionality in the web app so on first deploy it creates the user?
Thanks
Since web applications are not focused to "mass production", I can't tell if something like that is possible.
You can make an SQL script containing all your CREATE statements limited by "IF NOT EXISTS" to avoid deleting existing data. Hibernate can execute a sql script at the moment of creating the SessionFactory, if Hibernate finds a file named "import.sql" located in the classpath root, Hibernate will execute it. Also you can always edit the database.properties of an application already deployed without re-packaging the WAR.
Try Liquibase, http://www.liquibase.org/ . It does not allow your users to setup database connection details but it allows you to manage the database schema (create and update the schema) and default data.

Categories

Resources