Cannot get current session - spring

`
classpath:database.properties
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="org.entity">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven/>
<!-- not working -->
<!-- <context:component-scan base-package="org.service"></context:component-scan> -->
`
this is my spring configuration file. Problem is that i am not getting current session in my repository classes which are marked with #Repository, I have annotated my service methods with #Transactional annotation.
However all the autowiring is working fine, the only problem is when i try to get current session in repository classes, and if I use bean tag to instantiate my repository and service beans it works. What am i doing wrong here?

Use the OpenSessionInView Filter or similar, that way you should have a Session available during the request lifecycle (Controller/ View rendering).

Related

how to set sessionFactory instance only once for entire application?

I am Injecting sessionFactory Through xml file(applicationContext.xml) using this package org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.
is there anyway to set the sessionFactory only once for entire web application? how can I achieve this ?
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.mahesh</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
</props>
</property>
</bean>
Unless you tell Spring otherwise, it will create each bean once per context (i.e. they will be singletons). Your XML will do just that, so it already does what you want.

How could I use Spring's TransactionInterceptor with JPA?

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/>

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 - setting property value from JNDI

It is a bit of followup to my previous question
Spring and Hibernate - changing dialect
if for example I have this piece of .xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >
</prop>
</props>
</property>
</bean>
now, I wanted to set hibernate.dialect to value that is exposed by jndi by jdbc/dialect, but when I put <jee:jndi-lookup jndi-name="jdbc/MyDataSource"/> I am getting Invalid content was found starting with element 'jee:jndi-lookup'. No child element is expected at this so I suspect that I can't put any tags in prop.
So, is there any way I can insert jndi resource to this property?
Not completely sure, but you should be able to use Spring-EL here, like this:
<jee:jndi-lookup id="dialect" jndi-name="..." />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >#{dialect}</prop>
</props>
</property>
</bean>

Overriding global jta timeout in spring context in weblogic

Our Weblogic have global JTA timeout 30s, since our server is under high load setting such global timout to a bigger value can be critical for performace of the application.
So I want to override this JTA timeout with specific value in context of particular service.
I have root context for a number of webservices it has it's own global transaction manager:
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
<property name="transactionManagerName" value="javax.transaction.TransactionManager"/>
</bean>
annotation driven for this transation manager is on
<tx:annotation-driven proxy-target-class="true" transaction-manager="jtaTransactionManager"/>
and i have global hibernate session factory :
<bean id="parent.session.factory"
abstract="true"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
destroy-method="destroy"
p:dataSource-ref="dataSource.schedules">
<property name="hibernateProperties">
<props>
<prop key="hibernate.transaction.manager_lookup_class">
org.hibernate.transaction.WeblogicTransactionManagerLookup
</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.autocommit">false</prop>
</props>
</property>
</bean>
Then I have the context of my webservice where I want to override the jta timeout:
Inherited local transaction manager:
<bean id="localTransactionManager" parent="jtaTransactionManager">
<property name="defaultTimeout" value="120"/>
</bean>
Bean with javax TransactionManager interface for hibernate:
<bean id="localEETransactionManager" factory-bean="localTransactionManager"
factory-method="getTransactionManager">
<property name="transactionTimeout" value="120"/>
</bean>
Session factory for Hibernate with lolcaly overrided TransactionManager with EE interface (javax.transaction.TransactionManager)
<bean id="sessionFactory" parent="parent.session.factory">
<property name="jtaTransactionManager" ref="localEETransactionManager"/>
<property name="annotatedClasses">
<list>
<value>com.example.City</value>
<value>com.example.State</value>
<value>com.example.Country</value>
</list>
</property>
</bean>
And finally the DAO with localy overrided WebLogicJtaTransactionManager:
<bean id="addressDao"
class="com.example.AddressDataAccessObjectImpl"
p:entityClass="com.example.City"
p:dataSource-ref="dataSource"
p:sessionFactory-ref="sessionFactory"
p:transactionManager-ref="localTransactionManager"/>
But when I run my application hibernate throws Caused by: weblogic.transaction.internal.TimedOutException: Transaction timed out after 30 seconds
And rolls back the transaction.
Maybe someone know what I'm doing wrong with those overrides?
PS This override works only for root context, if I do somthing like that:
<bean id="jtaTransactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">
<property name="transactionManagerName" value="javax.transaction.TransactionManager"/>
<property name="defaultTimeout" value="120"/>
</bean>
But that is not what I whant because it is still global for all application.

Resources