Configuring Wildfly datasource for a JHipster application - spring

I am to deploy a JHipster application to Wildfly (v. 9.0.2) servers and need to use JNDI data sources.
The basic connection setup is pretty straightforward: I set up new data source in Wildfly's standalone.xml to mimic the out-of-the-box ExampleDS, then included a reference to it into application-dev.yml and slightly modified DatabaseConfiguration.java. Now, that works well.
The problem is that application-xxx.yml files also contain JPA configuration, and I don't know where to stick it into Wildfly (I guess it must be in Wildfly).
To be more precise, I have no idea where to move the following JPA parameters:
jpa:
database-platform
database
openInView
show_sql
generate_ddl
hibernate:
ddl-auto
naming-strategy
properties:
hibernate.cache.use_second_level_cache
hibernate.cache.use_query_cache
hibernate.generate_statistics
hibernate.cache.region.factory_class
I know that Wildfly advises this to be defined in persistence.xml, in which case, can I just drop it into the project?
Thank you in advance.

Related

How to make a Spring Boot project dependency use H2 database

I have a Spring Boot project (MAIN) which in turn depends on a (DAL) dependency.
DAL is where every Entity, Repository, Projection and Spring Data JPA related configurations are.
Now, MAIN project is a scheduler, and I need to make some integration tests over it. As always I want to use H2 database for the job in hands.
I have the H2 configuration under /test/resources/application.properties and this configuration have always worked if the JPA related classes were in the same project.
But in this specific case what is happening is that the H2 configuration are being ignored and the Integration Tests are writing into the real database.
Is possible to make the DAL to use H2 configurations?
For future reference, seting up H2 configuration for nested projects is very much possible.
The reason I was unable te setup this configuration rigth on the first attempt was because on DAL I have configured multiple data sources, as such the default h2 configuration wouldnt be enought.
So I had to make something like the folloing:
first-datasource.driver-class-name=org.h2.Driver
first-datasource.jdbcUrl=jdbc:h2:mem:testdb
first-datasource.username=sa
first-datasource.password=password
second-datasource.driver-class-name=org.h2.Driver
second-datasource.jdbcUrl=jdbc:h2:mem:testdb
second-datasource.username=sa
second-datasource.password=password

How to configure different data sources for local testing and deployment in Spring Boot Application

I am trying to find the best way to configure my Spring Boot Web application to easily switching between the following data sources for both local testing and deployment.
H2 in memory db. Local testing only.
Dev oracle. Local testing and deployment.
Prod oracle. Deployment only.
By local testing, I mean to test in IDE environment (Eclipse). Dev and prod oracle databases are set up on two remote servers.
After some research, there are different ways to switch from one data source to another.
Use Spring profile. Using H2 and Oracle with Spring Boot. Set up the following files in classpath, application.properties, application-h2. properties and application-dev.properties. While connections for h2 and dev are defined in corresponding properties files, spring.profiles.active is set in application.properties. My understanding is this property can be overridden during build process by specifying spring.profiles.active. However, it seems to be a JVM variable, how do I set it running maven?
Maven profile. Create multiple profiles in pom and a filter pointing to application properties files. The profile specified by -P option during maven build will determine which application properties file to look. However, according to maven application with multi environment configuration can't deploy on tomcat, this will generate multiple wars for different deployment. So method 1 is preferred. Plus, it does not apply to switching datasources while testing locally.
Persistence units. Define different persistence units for different data sources in persistence.xml. Use EntityManager by choosing a specific unit. Variation of this method include having a variable in unit names which is determined in application.properties.
JNDI lookup. Set up a jndi name in application.properties with spring.datasource.jndi-name. The actual database information including url and credentials will be specified in context.xml in the tomcat folder where the war will be deployed.
My mind is set on local testing environment. Gonna go with method 1. Switching between H2 in memory and oracle is so easy just by changing the property in application.properties. Since the testing is usually done in IDE, war does not need to be generated, although answers are welcome for run maven install with spring.profiles.active.
As far as deployment, JNDI is definitely the way to go. However, I am concerned that the two properties in application.properties: spring.profiles.active and spring.datasource.jndi-name may be conflicting with each other. If I have spring.profiles.active=h2 and then tried to deploy the war to prod server, does it try to connect to h2 based on the spring profile or to prod db based on jdni-name? What is the best practice to accommodate all scenarios with enough flexibility?
Also is a explicit configuration class for DataSource required such as Configure Mutiple DataSource in Spring Boot with JNDI? My understanding is application.properties and spring profile should be enough to handle it, right?
Definitely use Spring profiles.
You don't want to use Maven profiles as it creates different artifacts. Ask your QA/Release engineers how they feel about having different artifacts for different environments :). They wouldn't be happy.
H2 is what you want to use in CI server integration testing as well. Such integration testing is fast and easy.
Instead of changing profile in application.properties, consider defining profile via command line parameter. So that configuration file changes are not required to run your application in different profiles.

Spring Boot Tests within a Container

I have coded a Spring Boot based web application, which is expected to be run in WildFly server. The applications runs great, but the issue is with testing.
I have the database connections, caching and transaction management dealt by the server. Now, I need to be able to test them. While I was able to get through database connection problem through a mock JNDI connection and the transaction management, I'm not sure how to deal with testing of the caching.
One solution is to use Arquillian project. But, either this project is unable to recognize Spring Boot/ I'm doing something wrong, which is causing me pain to test the application.
Can someone please suggest on solving the issue? Below are my hibernate specific properties
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.properties.hibernate.cache.region.factory_class=org.jboss.as.jpa.hibernate4.infinispan.InfinispanRegionFactory
spring.jpa.properties.hibernate.cache.infinispan.cachemanager=java:jboss/infinispan/container/hibernate
spring.jpa.properties.hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.cache.use_query_cache=false
spring.jpa.properties.hibernate.hbm2ddl.auto=none
spring.jpa.properties.hibernate.generate_statistics=true
spring.jpa.properties.hibernate.cache.infinispan.statistics=true
spring.jpa.properties.hibernate.search.default.directory_provider=infinispan
spring.jpa.properties.hibernate.search.infinispan.cachemanager_jndiname=java:jboss/infinispan/container/hibernate
I would suggest creating a separate configuration for tests. This configuration would contain a definition of a TransactionManager bean - here is an example from other post. The next step is to provide your own implementation of TransactionManagerLookup and applying it to Transport configuration - as described in the manual.

Spring Boot testing using in memory db

I have created a Spring web project using Spring Boot.
Would like to understand the practice around testing. I require an in memory embedded database say hsql or h2 for my junits with initial schema.sql.
And on the main application the database could be say mysql or oracle
In a non Spring Boot project, we would normally have a separate applicationcontext.xml one which is referred by the web app and for testing we would use applicationContext-text.xml
Now, in Spring boot as everything is created automatically and Spring Boot is opiniated too. Would like to know how do I setup having an embedded inmemory db for Junits and an external db like MySQL for the application.
One solution I can think of is using Profiles. with 2 properties file application.properties and application-test.properties. and use test profile for my junits.
Any recommendation on the approach I should take.
A profile is, indeed, the recommended approach. What I would do is probably make the in-memory implementation the "default" profile (it's harmless, in the sense that you never change any real data, so it's better to make that the default in case someone accidentally runs it against a real database). Personally, I prefer to put all the external configuration in a single application.yml file, but that's really up to you. In the external configuration you need to supply a valid driver class and URL, e.g.
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:test;MODE=PostgreSQL
schema: classpath:/schema.sql
---
spring:
profiles: local
datasource:
url: jdbc:postgresql://localhost/test
username: root
password: changeme
driverClassName: org.postgresql.Driver
schema:
(Note that H2 has a postgres compatibility mode, so it is really nice as a complement to postgres in production.)

Configure JDBC oracle specific property v$session.program using Jboss and JPA (hibernate)

I'd like to set the v$session.program Oracle property in order to have information available in the session table. I'm using JPA with a jndi XA datasource created with an oracle-xa-ds.xml deployed in the deploy folder of Jboss, and therefore I haven't access to the constructor of the Connection.
I have access to the Connection object, in JPA 2 using unwrap, in JPA 1 by casting JPA to Hibernate classes, but there are no properties setter (only Client Info properties that are the way to proceed starting JDBC 4.0).
So my question is, using JPA (with Hibernate) using Jboss 4.2 :
Is it possible to configure the v$session.program in the persistence.xml ?
Is it possible to configure the v$session.program in the oracle-ds.xml ?
Is their any other approach to the solution ?
Thank you for any valuable comments and answers !
I had the same Problem today, after much fiddeling and reading documentation finally I had the Eureka moment:
Add following parameter:
<xa-datasource-property name="connectionProperties">v$session.program=YourUniqueName</xa-datasource-property>
Thats all.
I'm pretty sure this must be documented somewhere but here is what we can find in the JBoss wiki:
How To Specify "PROGRAM" Oracle Connection Property
JBoss Version: JBoss 4.0.3 SP1, Oracle DB Version: 10g
To be able to distinguish the JDBC
connections on the Oracle server side,
which are created by different JBoss
instances, Oracle's PROGRAM connection
property might be set within the
Oracle specific JDBC datasource config
file by using the following tags:
<connection-property name="v$session.program">ADistinguishedNameForPROGRAMProperty</connection-property>
i.e.
...
<connection-url>AConnectionURL</connection-url>
<connection-property name="v$session.program">ADistinguishedNameForPROGRAMProperty</connection-property>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
...
That way the DBAs can have proper
granularity in:
AWRs
v$session view
Other tools which are checking/evaluating PROGRAM connection
property

Resources