Illogical Results from Cucumer/Arquillian Test with H2 - jpa

I am having an issue where I am attempting to create a Cucumber/Arquillian test for a new service that performs a batch update with JPQL. Everything seems to work correctly, except my #Then code validating the change.
I am setting up my test data in my feature file and it gets added to the H2 database that is created in memory for each test. When the batch update runs it reports back the expected update count based on that data. But when I retrieve one of the objects that should have been updated, the data on that object appears to be unchanged.
Please note: When I execute the service call in my application against our Oracle database it works correctly and the table is updated as expected. The problem seems to be with caching on the H2 in memory database.
My datasource that gets deployed to JBoss by Arquillian is:
<datasource enabled="true"
jndi-name="jdbc/arquillian"
pool-name="ArquillianEmbeddedH2Pool">
<connection-url>
jdbc:h2:mem:arquillian;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS TEST_DB
</connection-url>
<driver>h2</driver>
</datasource>
My Cucumber test defines #PersistenceContext(unitName = "localH2-testDB")
My persistence.xml contains:
<persistence-unit name="localH2-testDB">
<jta-data-source>jdbc/arquillian</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.cache.use_second_level_cache" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
</properties>
</persistence-unit>
I don't know what other settings for the H2 database I can change to try and eliminate any caching.

It turns out the problem is inherent to how batch updates are handled in JPA. They do NOT update the persistence cache as one might expect. So the tables were updated correctly, but when the object was re-queried, it didn't have the updates. This is why my test was failing (everything was inside the same transaction) and my deployed code worked (separate transactions performing the update and re-querying the data).

Related

How to define - environment specifc mongo db configuration in play framework with JpaApi?

I am working on a project where I am using play framework along with mongo db. As of now I have hardcoded the value for local db connection in persistence.xml file and given the jpa.default value as persistenceUnitName and I am using the play's JpaApi for the db operations (which inherently uses the entity manager).
I am not able to identify how to define environment (prod, dev, stage) specific db properties like host, url etc. in application.conf or any other file.
application.conf entry - jpa.default=my-local-jpa
<?xml version="1.0" encoding="UTF-8"?>
<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="my-local-jpa" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<non-jta-data-source>DefaultDS</non-jta-data-source>
<properties>
<property name="hibernate.ogm.datastore.provider"
value="org.hibernate.ogm.datastore.mongodb.impl.MongoDBDatastoreProvider"/>
<property name="hibernate.ogm.datastore.host"
value="127.0.0.1"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="hibernate.ogm.datastore.database" value="my_db"/>
<property name="hibernate.ogm.datastore.safe" value="true"/>
<property name="hibernate.ogm.datastore.create_database" value="true" />
</properties>
</persistence-unit>
</persistence>
There would be different solutions. It depends on your environment.
If you are using WildFly / JEE container, you can configure a WildFly NoSQL subsystem, providing there the references to the remote datastore. It would be the equivalent of a SQL datasource, but for a NoSQL datastore. See Using WildFly NoSQL
If you are using a web container, there would be different strategies.
You can create different war(s), one for each environment, for instance using maven profiles.
Alternatively, you can configure your Spring context in order to use an external property file. See this question.
If you deploy it in a PASS, such as OpenShift, you can mount the persistence.xml file as a config map. See Config Map - OpenShift doc

Configure Spring batch admin to use db2 database

In order to configure spring batch admin UI to use db2 database, I referred the Admin UI documentation which says "launch the application with a system property -DENVIRONMENT=[type]." I understand that "-DENVIRONMENT=db2" should be kept in some file. I tried by keeping in batch-default.properties file, but that did not work. Since I am using WLP(liberty server), tried by keeping in server.xml file, no help. Still in the console I see env-context.xml file from batch admin is still loading batch-hsql.properties file(default configuration).
My job is written using Spring Boot so I put property, ENVIRONMENT=db2 in application.properties and add a new file - batch-db2.properties at same location as application.properties.
Few compulsory properties will be needed there like - you need to try an experiment,
batch.job.configuration.package=
batch.drop.script=classpath*:/org/springframework/batch/core/schema-drop-db2.sql
batch.schema.script=
batch.business.schema.script=
#Copied from batch.properties of spring-batch-admin-manager API project
batch.jdbc.testWhileIdle=false
batch.jdbc.validationQuery=
batch.data.source.init=false
batch.job.configuration.file.dir=target/config
batch.job.service.reaper.interval=60000
batch.files.upload-dir=/sba/input
I had put DB connection information too but later I moved to JNDI by overriding file - data-source-context.xml in META-INF\spring\batch\override like below,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="ConnectionPool" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
ConnectionPool is db connection pool JNDI name from server.
Keeping configurations in your code lets you freely move your app to different servers without asking for server specific configurations first.
Not really familiar with liberty server, but the link below says that system properties need to be added to jvm.options file. See link below :
https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_admin_customvars.html

safety disconnect oracle db from a jboss application

I'm maintenance a old system it run in a jboss container,and it use ibatis and spring access an oracle db. Now this system's db related functions are discarded and the db will be closed. How should i do to safety disconnect this system with db (assuming the application code can deal with all exceptions except SqlException)
the key configuration is as follows:
xxx-ds.xml:
<datasources>
...
</datasources>
daoContext.xml:
<jee:jndi-lookup id="oracleSource" jndi-name="java:/DefaultDS"/>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="META-INF/sql-map-config.xml"/>
</bean>
<bean id="xxxx" class="path.to.class.xxxxDao">
<property name="dataSource" ref="oracleSource"/>
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
path.to.class.xxxxDao is extends org.springframework.orm.ibatis.support.SqlMapClientDaoSupport and implements db access methods.
Replace jndi data source with a mock db

Orbeon Forms Postgres DB connection

I have tried to save the Orbeon forms in Postgres DB. I created data bases persistence layer but still forms are stored in the default eXist DB. I have created context(Data Base source) in the "Server.xml" from the tomcat7 and I have created "properties-local.xml" file in the Orbeon4.8 (orbeon/src/resources/config/properties-local.xml). My reference is this.
Some one help me how to save the orbeon forms in Postgres DB?
Once the database resource is configured, you can tell Orbeon to use it by adding a like like this to your properties-local.xml:
<property as="xs:string" name="oxf.fr.persistence.provider.*.*.*" value="postgresql"/>
You can see the default configuration for the resource names Orbeon accepts out of the box in properties-form-runner.xml.
Exist will still be used for the example forms. But you can disable it with:
<property as="xs:boolean" name="oxf.fr.persistence.exist.active" value="false"/>
If you need to define a resource with a name other than the default names (e.g. postgresql), you will need to provide more information as shown below (remember to replace all occurances of pg_other_name with your resource name).
<property as="xs:string" name="oxf.fr.persistence.provider.*.*.*" value="pg_other_name"/>
<property as="xs:anyURI" name="oxf.fr.persistence.pg_other_name.uri" value="/fr/service/postgresql"/>
<property as="xs:string" name="oxf.fr.persistence.pg_other_name.datasource" value="pg_other_name"/>
<property as="xs:boolean" name="oxf.fr.persistence.pg_other_name.create-flat-view" value="true"/>
<property as="xs:boolean" name="oxf.fr.persistence.pg_other_name.autosave" value="true"/>
<property as="xs:boolean" name="oxf.fr.persistence.pg_other_name.permissions" value="true"/>
<property as="xs:boolean" name="oxf.fr.persistence.pg_other_name.versioning" value="true"/>
I'm not sure what you mean by "created context in the Server.xml".
In tomcat's server.xml you should have a datasource defined. For example:
<GlobalNamingResources>
<Resource
name="jdbc/postgresql"
auth="Container"
type="javax.sql.DataSource"
initialSize="3"
maxActive="10"
maxIdle="20"
maxWait="30000"
driverClassName="org.postgresql.Driver"
validationQuery="select 1"
testOnBorrow="true"
poolPreparedStatements="true"
username="orbeon"
password="orbeon"
url="jdbc:postgresql://server:5432/database?useUnicode=true&characterEncoding=UTF8&socketTimeout=30&tcpKeepAlive=true"/>
</GlobalNamingResources>
Then in tomcat's context.xml you should have a ResourceLink for the datasource:
<ResourceLink global="jdbc/postgresql" name="jdbc/postgresql" type="javax.sql.DataSource"/>
properties-local.xml should be either in webapps/orbeon/WEB-INF/resources/config or in an external directory you have defined in tomcat's context.xml, for example:
<Parameter name="oxf.resources.priority.0" override="false" value="org.orbeon.oxf.resources.FilesystemResourceManagerFactory"/>
<Parameter name="oxf.resources.priority.0.oxf.resources.filesystem.sandbox-directory" override="false" value="C:/orbeon_resources"/>
In that case the properties-local.xml should be here: C:\orbeon_resources\properties-local.xml. See Storing configurations outside of the Orbeon Forms war file.
Also, make sure you have performed the Orbeon Forms setup. That is general database configuration, not specific to postgresql or any other database implementation. The examples they give are for oracle but you can just replace oracle with postgresql.

Can't make JTA work on jboss AS7.1 with spring 3.1

We're trying to configure a Spring application to work with JTA transactions. It is not like it is failing, but everything we tried just do the select and ignores our persistence operations.
As you can see in the following logs, even though the save statement run there’s no insert statement, neither an exception, neither an error/warning logs
server.log
DEBUG [xxxx.xxxx.persistence] (http--0.0.0.0-8080-1) execute $Proxy363.save(whatever.core.entities.XxxAssignedTime#51a8c542)
DEBUG [org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor$CustomAnnotationTransactionAttributeSource] (http--0.0.0.0-8080-1) Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (http--0.0.0.0-8080-1) Returning cached instance of singleton bean 'transactionManager'
DEBUG [org.springframework.transaction.jta.JtaTransactionManager] (http--0.0.0.0-8080-1) Participating in existing transaction
INFO [xxxx.xxxx.impl.BpmnUserBusinessImpl] (http--0.0.0.0-8080-1) Last Assigned Time Updated to *******, Thu Sep 06 15:48:48 ICT 2012
It is like thought the application server thinks everything went right. Whereas if you check the table no insertion or update was made.
The datasource that we intend to use with a spring application is successfully used with JTA transactions by a Java EE war application running in the same application server.
Since we have no idea where the problem might be I'd like to put it all in full context.
spring 3.1.2-RELEASE
hibernate 4.1.5.Final
spring-data-jpa 1.1.1.Final
apache axis 1.4
jboss AS7.1
DB: oracle 10g
We've been trying all sorts of crazy configuration in order to try to make it work, so I will just copy here the simplest one.
standalone.xml
<datasource jta="true" jndi-name="java:jboss/datasources/EngineDS" pool-name="EngineDS" enabled="true" use-java-context="true" use-ccm="true">
<connection-url>jdbc:oracle:thin:#server:port:****</connection-url>
<driver>oracle</driver>
<transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>****</user-name>
<password>****</password>
</security>
<statement>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<share-prepared-statements>true</share-prepared-statements>
</statement>
</datasource>
<drivers>
<driver name="oracle" module="com.oracle.ojdbc6">
<xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
</driver>
</drivers>
web.xml
[...]
<resource-ref id="DS">
<res-ref-name>EngineDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<mapped-name>java:jboss/datasources/EngineDS</mapped-name>
</resource-ref>
[...]
spring-jpa-config.xml
<jpa:repositories base-package="whatever.core.repositories" />
<jee:jndi-lookup id="dataSource" jndi-name="EngineDS" cache="true" resource-ref="true" expected-type="javax.sql.DataSource"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="whatever.core.entities" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${hibernate.show_sql}" />
<property name="generateDdl" value="${jpa.generateDdl}" />
<property name="databasePlatform" value="${jpa.dialect}" />
</bean>
</property>
</bean>
<tx:annotation-driven />
<tx:jta-transaction-manager />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
We had made in the past jboss AS5/Spring 3.0 applications to work with JTA transactions, so it is not like is my first time doing this, and we've been looking around all possible blogs and open source projects I could find. Yet anything that seem to work smoothly to everybody seems to be ignored in my application. I'm sure it should be that we're missing something really stupid somewhere but we've tried so far 70+ different configurations and none seem do a simple insert that otherwise work when not trying JTA.
(The fact that we use axis 1.4 might be relevant or not, but I'd like to tell because our application only triggers actions after a web service call). At this point we are starting to believe in configuration paranormal activity...
Any clue anyone?
It turned out that the configuration above wasn't working due to the fact that I was using org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.
To have JTA I should lookup for the JBoss container-managed entity manager spring-jpa.config.xml:
<!-- lookup the container-managed JPA-EMF -->
<!-- the JNDI name is specified in META-INF/persistence.xml -->
<!-- SEE: https://docs.jboss.org/author/display/AS71/JPA+Reference+Guide#JPAReferenceGuide-BindingEntityManagerFactorytoJNDI -->
<jee:jndi-lookup id="defaultPu" jndi-name="java:jboss/defaultPu" />
And in the META-INF/persistence.xml you bind the Entity Manager Factory into JNDI:
<!-- bind the EMF in JNDI -->
<property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/defaultPu" />
So, I let Jboss bootstrap JPA, and in such way the JTA transactions work!