How could I use Spring's TransactionInterceptor with JPA? - spring

I have an existing project using Spring 3 and Hibernate 3. I have the following code in order to "safe-guard" the database consistency. If I'm going to convert the project into JPA, how could I resolve the transactionManager property inside the transactionInterceptor bean since JPA using persistence.xml and doesn't make use of dataSource and sessionFactory?
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="save">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>

Try using a configuration similar to the below xml snippet. This has been tested with Hibernate 4, but I would expect it to work with version 3 as well.
<!-- EntityManagerFactory configuration that doesn't need a persistence.xml -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="${jpa.entity.packages}">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="${hibernate.show_sql}"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- Scans for classes/methods with #Transactional annotation to apply the
transaction management aspect (TransactionInterceptor) on them. -->
<tx:annotation-driven/>

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?

spring bean optional property

I am using a data source defined in tomcat in my spring configuration as shown in the below xml.
It can happen sometimes that this data source may not be defined in the context.xml of tomcat.
In such cases , the context initialization fails since myDS is not found.
Is it possible to configure the datasource as optional so that application initialisation is not impacted ?
There can be a run time error when this data source is accessed , which is acceptable
<bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/myDS"/>
</bean>
<bean id="myEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="packagesToScan" value="com..XX.XX" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="#{systemProperties['showSql'] == null ? 'true' : systemProperties['showSql'] }" />
</bean>
</property>
<property name="persistenceUnitPostProcessors">
<list>
<ref bean="wrkflw-punitpostprocessor" />
</list>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">#{systemProperties['dbDialect']}</prop>
</props>
</property>
</bean>
Thanks
Muhad
You may check the DelegatingDataSource, you could encapsulate the logic to load the datasource from JNDI within its instantiation. For your application there will be always a DataSource there, but in some cases (whenever its not able to load the DataSource from JNDI) there is no delegation.

spring transaction and aop

I'm writing a simple application with spring, I defined my daos and services with spring annotations and defined hibernate as the orm and transaction manager like this:
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:env/hibernate.cfg.xml</value>
</property>
<property name="packagesToScan">
<list>
<value>com.skyfence.management.cm.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
like you can see I'm using annotations for the transactions management,
Until this point everything works fine.
Then I added a Logger Aspect to add log4j printouts before and after every method so I added the following to my applicationContext.xml
<aop:aspectj-autoproxy />
and created a new annotated aspect class:
#Aspect
#Component
public class LoggingAspect
{
}
The problem is that from that point hibernate is no longer working and I'm getting the following exception:
org.hibernate.HibernateException: No Session found for current thread
I suspect that somehow by adding the aspect the transactions stopped working but I have no idea how to solve it
Any help will be appreciated,

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