Spring and H2 or Derby - Multiple Transactions - spring

I have a project that uses Spring. In local environment I use Derby (with Jetty) that gives me the perfomance that I need in this environemnt (in production is used Oracle and Weblogic).
The problem is that I need new transactions in some specific operations. This specific operations use the annotation #Transactional(propagation=REQUIRES_NEW).
The problem is that with Derby when comes the time that a new transaction was supposed to be createad, the operation hangs and due time out. I tried to use H2 but in the same moment of creation of the new transaction, the database is restarted, meaning, the tables are dropped and are created again.
Derby and H2 don't support multiple transactions? I can't find anything that tells me the opposite.
My database configuration is:
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
<property name="persistenceUnitName" value="ORACLE_PU"/>
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.eclipse.persistence.platform.database.DerbyPlatform"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="shared-cache-mode">NONE</prop>
<prop key="eclipselink.cache.shared.default">false</prop>
<prop key="eclipselink.query-results-cache">false</prop>
<prop key="eclipselink.weaving">false</prop>
<prop key="eclipselink.ddl-generation">${oracle.eclipselink.ddl.generation}</prop>
<prop key="eclipselink.ddl-generation.output-mode">database</prop>
<prop key="eclipselink.create-ddl-jdbc-file-name">create-tables.sql</prop>
<prop key="eclipselink.drop-ddl-jdbc-file-name">drop-tables.sql</prop>
<prop key="eclipselink.application-location">target/generated-sources/database</prop>
<prop key="eclipselink.jdbc.cache-statements">true</prop>
<prop key="eclipselink.custom.sql.import.file">${oracle.eclipselink.custom.sql.import.file}</prop>
...
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:target/memory;create=true"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
My attempt with H2 is similar but with the proper configurations. In weblogic environemnt everything works as expected.
What I'm missing?

It seems you are using derby in embedded in-memory mode. AFAIK, both derby and H2 support concurrent transactions only in standalone mode, when you connect through a TCP connection, but not in embedded mode. Try running derby as a seperate procese and change your database to connect to it.

Related

Spring JPA with Hibernate Persistence provider and setting javax.persistence.schema-generation properties

I am using Spring JPA with Hibernate as the persistence provider and Derby as the database. (Specifically, the version of jars are Spring version 4.3.4, Hibernate Entity Manager version 5.2.10 and Derby 10.13.1.1).
I want to check the correct approach for
The schema generation in the JPA properties.
How to use the schema generated from step 1 to create the database schema.
(Note this is not a Spring Boot application.)
My application config file is this:
<context:annotation-config />
<context:component-scan base-package="org.service"/>
<context:component-scan base-package="org.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan">
<array>
<value>org.model</value>
<value>org.repository</value>
<value>org.service</value>
</array>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:C:\\tmp\\testdb"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<jpa:repositories base-package="org.repository"></jpa:repositories>
For step (1), my investigation and testing suggests that it's possible to set the persistence schema generation inside the jpaProperties property as follows:
<prop key="javax.persistence.schema-generation.scripts.action">create</prop>
<prop key="javax.persistence.schema-generation.scripts.create-target">create.sql</prop>
This will generate a create.sql file. For step (2), I used
<prop key="javax.persistence.schema-generation.create-source">script</prop>
<prop key="javax.persistence.schema-generation.create-script-source">create.sql</prop>
<prop key="javax.persistence.schema-generation.database.action">create</prop>
However do I need to still set create=true in the URL jdbc:derby:C:\tmp\testdb? Do the above lines for javax.persistence.schema-generation assume the database has been created?

autocreate schema with jdo, spring and H2 with datanucleus

I Recently migrated to using the spring framework for DI - working fine. I'm injecting a persistence manager which also works fine. On a new install, I get:
SEVERE: Required table missing .... Either your MetaData is incorrect, or you need to enable "datanucleus.autoCreateTables"
Fair enough, I'm not enabling autocreate tables.
I create my persistence manager like this in the spring context.xml per the doc:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:./thedbpath.db;MV_STORE=FALSE;MVCC=FALSE;FILE_LOCK=NO"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="pmf" class="org.datanucleus.api.jdo.JDOPersistenceManagerFactory" destroy-method="close">
<property name="connectionFactory" ref="dataSource"/>
<property name="nontransactionalRead" value="true"/>
</bean>
everything works - but i can't figure out where to set the datanucleus.autoCreateTables
This normally would be set in the persistence.xml - I don't see where to put datanucleus properties in the spring context.xml. Thanks in advance
edit: thanks to the answer below, this was the correct config:
<bean id="pmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
<property name="jdoProperties">
<props>
<prop key="javax.jdo.PersistenceManagerFactoryClass">
org.datanucleus.api.jdo.JDOPersistenceManagerFactory
</prop>
<prop key="javax.jdo.option.ConnectionURL">jdbc:h2:./database/db;MV_STORE=FALSE;MVCC=FALSE;;FILE_LOCK=NO</prop>
<prop key="javax.jdo.option.ConnectionUserName">sa</prop>
<prop key="javax.jdo.option.ConnectionPassword"></prop>
<prop key="javax.jdo.option.ConnectionDriverName">org.h2.Driver</prop>
<prop key="org.jpox.autoCreateSchema">true</prop>
<prop key="org.jpox.identifier.case">PreserveCase</prop>
<prop key="datanucleus.autoCreateTables">true</prop>
</props>
</property>
</bean>
This page
http://www.datanucleus.org/products/accessplatform_3_0/guides/jdo/springframework/index.html
has a "jdoProperties" property that can be used to specify JDO implementation-specific properties. Maybe try that?

Intellij JPA Console with persistence.xml

I'm setting up a xml-free persistence JPA/Hibernate 4+/Spring 3+ using Intellij 13+. When I try to execute a query in the jpa console, I get the following error:
javax.persistence.PersistenceException: Unable to build entity manager factory
java.lang.RuntimeException: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.dialect.PostgreSQL9Dialectt.PostgreSQL9Dialect] as strategy [org.hibernate.dialect.Dialect]
With the default postgres dialect, I get the same error.
Any idea what's going on?
Configuration extract:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.wikiz.service.model.rep" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="jadira.usertype.autoRegisterUserTypes">true</prop>
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
</bean>
And variables:
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create
Ok here it is.
I am not 100% sure if this helps your case but I think this is what you need to do:
Add a jdbc connection to your database from the database tab (usually to your right)
Then add the hibernate facet to your module
Go to modules (alt+ctrl+shift+s) then add it:
Now you have enabled the persistance tab on your left (usually) and you can assign a datasource to your
Now add the hibernate configuration but you have to add the xml file of hibernate. I haven't tried it with just adding the spring application context instead of hibernate.cfg.xml. Maybe it will work...
Now you have enable the presistance configuration for intellij and you can assign a datasource to it.
Select that datasource that you want and you will be able to use the JPA console with your jpa POJOs and HSQLs

Spring JPA Hibernate valid configuration not working with JdbcTemplate?

I do have a working configuration in Spring for JPA with Hibernate provider:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<tx:annotation-driven mode="aspectj"/>
<bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"/>
This configuration is working in a small Spring based web-app.
But, when I insert the same configuration into an other existing Spring based web-app, I do get the following exception:
javax.persistence.TransactionRequiredException: no transaction is in progress
I think it has some conflict with Spring JDBC Templates:
<bean id="mysqlTemplate"
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
How can I get this to work side by side?
"no transaction in progress" just means you're trying to use an EntityManager somewhere and you didn't start a transaction first. The stack trace will tell you exactly where that is. Since you're using annotation-driven transactions, just make sure you have an appropriate #Transactional somewhere up the call chain from the location where the exception is happening.

Hibernate temporary datastore

I doing unit testing on spring-hibernate DAOs... configured using
#ContextConfiguration(
locations = {
"classpath:test-applicationContext.xml"
})
but it looks like its transacting with the actual database.
How do I use a temporary datastore with out working on actual database
Define your data store on a separate file and include that xml file with your mail application xml. When testing, include a separate xml file to hold your data store pointing to another database such as hsql. Then this will be the data source referred to by your main applicationContext.xml.
Thanks Guys I used H2 and got it working:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url"
value="jdbc:h2:mem:processdb;INIT=RUNSCRIPT FROM 'classpath:create.sql'" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="stateDAO" class="com.tutorial.jquery.dao.impl.StateDAOImpl"></bean>
<bean id="stateService" class="com.tutorial.jquery.service.impl.StateServiceImpl"></bean>

Resources