Using Spring's LocalTaskExecutorThreadPool with Quartz - spring

Im trying to use the LocalTaskExecutorThreadPool with quartz but when i try to use it as the quartz taskexecutor I get this error.
ERROR:
arg.springframework.beans.TypeMismatchException: Failed to convert property value of type
[org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool] to required type [org.springframework.core.task.TaskExecutor] for property 'taskExecutor'.
Spring Config
<bean id="taskExecutor" class="org.springframework.scheduling.quartz.LocalTaskExecutorThreadPool">
</bean>
<bean id="schedulerTarget" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" scope="singleton" lazy-init="false">
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<property name="dataSource">
<ref bean="dataSrcBean"/>
</property>
<property name="transactionManager">
<ref bean="txManager" />
</property>
<property name="taskExecutor">
<ref bean="taskExecutor" />
</property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.jobStore.class">org.springframework.scheduling.quartz.LocalDataSourceJobStore</prop>
<prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.MSSQLDelegate</prop>
<prop key="org.quartz.jobStore.selectWithLockSQL">SELECT * FROM {0}LOCKS WHERE LOCK_NAME = ?</prop>
<prop key="org.quartz.plugin.shutdownhook.class">org.quartz.plugins.management.ShutdownHookPlugin</prop>
<prop key="org.quartz.plugin.shutdownhook.cleanShutdown">true</prop>
<prop key="org.quartz.scheduler.instanceName">Sched1</prop>
<prop key="org.quartz.scheduler.instanceId">1</prop>
<prop key="org.quartz.scheduler.rmi.export">false</prop>
<prop key="org.quartz.scheduler.rmi.proxy">false</prop>
</props>
</property>
</bean>
The whole purpose of this is to have Spring control any connection quartz makes. I already have a Spring transaction manager being used by the scheduler but its seems the scheduler will leave sleeping connections on my db.
Thnaks

You shouldn't use LocalTaskExecutorThreadPool yourself - SchedulerFactoryBean uses this internally to wrap a TaskExecutor in Quartz's ThreadPool interface.
SchedulerFactoryBean expects a taskExecutor object to be injected. You need to decide which implementation of TaskExecutor that you want to use.

Related

Upgrading hibernate version to 5.4 gives me 'no transaction is in progress'

I am trying to upgrade spring version(4.3) and hibernate version(5.4) and I am getting 'no transaction is in progress' exception, I am using HibernateTransactionManager and also I have tried with setting hibernate.transaction.coordinator_class to 'jta' as well as 'jdbc'. my configs are below
<bean id="MyTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="myAppSessionFactory"/>
</bean>
<bean id="myAppSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="true">
<property name="mappingLocations">
<list>
<value>classpath:/mappings/UserGroupMembershipPOJO.hbm.xml</value>
<value>classpath:/mappings/UserGroupPOJO.hbm.xml</value>
<value>classpath:/mappings/UserDetailsPOJO.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.use_outer_join">false</prop>
<prop key="hibernate.jdbc.fetch_size">5</prop>
<prop key="hibernate.jdbc.batch_size">0</prop>
<prop key="hibernate.transaction.coordinator_class">jta</prop>
</props>
</property>
<property name="dataSource">
<ref bean="myDataSource"/>
</property>
</bean>
Hibernate now conforms with the JPA specification to not allow flushing updates outside of a transaction boundary. To restore 5.1 behavior, allowing flush operations outside of a transaction boundary, set hibernate.allow_update_outside_transaction=true.
hibernate-orm/migration-guide.adoc at 5.2 ยท hibernate/hibernate-orm

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.

Cannot get current session

`
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).

Reading Enviornment variables in Spring application context

I have three enviornment variables.
MY_TOPIC
MY_CONTEXT_FACTORY
MY_LDAP
First one - MY_TOPIC works like this
<bean id="myPublishTopic" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
<property name="jndiName">
<value>fxClientDestinationUID=${MY_TOPIC}</value>
</property>
</bean>
Last two does not work reading from enviornment variables. How do it make this work?
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${MY_CONTEXT_FACTORY}</prop>
<prop key="java.naming.provider.url">${MY_LDAP}</prop>
</props>
</property>
</bean>
Last two works only if i read it from properties file which i want to avoid.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/classes/springConfig-devel.properties</value></property>
</bean>
If you are using Spring 3+ you can use Spring Expression Language.
Use #{systemEnvironment['NAME']} to access environment variables
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">#{systemEnvironment['MY_CONTEXT_FACTORY']}</prop>
<prop key="java.naming.provider.url">#{systemEnvironment['MY_LDAP']}</prop>
</props>
</property>
</bean>
Use #{systemProperties['value']} for Java system properties.

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