Dependency Injection is working at Mule application startup. Objects are getting null, when a request received and Failing by throwing NullEx - spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<context:component-scan base-package="com.example.hibernate" />
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/emp" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire = "byname">
<property name="dataSource" ref="myDataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.connection.pool_size">20</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Departments.hbm.xml</value>
</list>
</property>
</bean>
<bean id ="employeeDAO" class = "com.example.hibernate.dao.EmployeeDAOImpl" autowire = "byname"/>
</beans>
And here is Mflow
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:context="http://www.springframework.org/schema/context"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<spring:beans>
<spring:import resource="DBConfigurations.xml" />
</spring:beans>
<data-mapper:config name="getEmployeeDetailsReq"
transformationGraphPath="getEmployeeDetailsReq.grf" doc:name="getEmployeeDetailsReq" />
<data-mapper:config name="getEmployeeDetailsResponse" transformationGraphPath="getEmployeeDetailsResponse.grf" doc:name="getEmployeeDetailsResponse"/>
<flow name="EmployeeDetailsFlow" doc:name="EmployeeDetailsFlow">
<http:inbound-endpoint exchange-pattern="request-response"
address="http://localhost:9090/getEmployeeDetails" doc:name="Receive" />
<object-to-string-transformer doc:name="Object to String" />
<data-mapper:transform config-ref="getEmployeeDetailsReq"
doc:name="getEmployeeDetailsReq" />
<component doc:name="GetTemplateInfo"
class="com.example.hibernate.dao.GetEmployeeDetails" />
<data-mapper:transform config-ref="getEmployeeDetailsResponse" returnClass="java.lang.String" doc:name="getEmployeeDetailsResponse"/>
<json:object-to-json-transformer
doc:name="Object to JSON" />
</flow>
</mule>
Here is my Java Component
package com.example.hibernate.dao;
import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.example.hibernate.Employee;
public class GetEmployeeDetails implements Callable{
#Autowired
#Qualifier("employeeDAO")
private EmployeeDAO dao;
#Override
public Object onCall(MuleEventContext eventContext) throws Exception {
Employee emp = (Employee)eventContext.getMessage().getPayload();
return dao.getEmployeeDetailsById(emp.getId());
}
}
I have tried to inject object using autowire and also by using spring setter injection.
In both the scenarios by adding Syso in constructor and setter method I have noticed, that the object is getting injected at the time of application start up. Sysos are getting printed in the console.
When I send a request, object is getting null and failing by throwing NullPointerException.
Even the same is not working in Service classes as well.
Can any one help me out?
Thanks in advance.

Do you mind trying the following?
Add this to DBConfiguration.xml:
<bean id="getEmployeeDetails"
class="com.example.hibernate.dao.GetEmployeeDetails" />
And change the flow to:
<component>
<spring-object bean="getEmployeeDetails"/>
</component>

Related

Hikari configuration in xml for Master/Slave Datasources

Presently I have a datasource configured using Hikari in my applicationContext.xml. I want to configure in the same xml file for a master/slave kind of datasource.
Also how do I establish a new entity manager factory in the same ?
My applicationContext.xml which is configured for 1 datasource(read as master) looks like:
<xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
<context:spring-configured />
<context:component-scan base-package="com.lkart.dao">
</context:component-scan>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<property name="dataSourceClassName" value="${database.driverClassName}" />
<property name="maximumPoolSize" value="10" />
<property name="maxLifetime" value="1800000" />
<property name="idleTimeout" value="600000" />
<property name="connectionTimeout" value="60000" />
<property name="dataSourceProperties">
<props>
<prop key="url">${database.url}</prop>
<prop key="user">${database.username}</prop>
<prop key="password">${database.password}</prop>
<prop key="prepStmtCacheSize">250</prop>
<prop key="prepStmtCacheSqlLimit">2048</prop>
<prop key="cachePrepStmts">true</prop>
<prop key="useServerPrepStmts">true</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
How do I configure this xml for adding a slave datasource ?
specify url as described in mysql connector-j documentation

Consuming from Kafka failed; nested exception is java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Iterator is in failed state

Below is my consumer and producer XML I can send message successfully on topic and can read from consumer but in console I am getting below error:
10823 [taskExecutor-30] ERROR
org.springframework.integration.handler.LoggingHandler -
org.springframework.messaging.MessagingException: Consuming from Kafka
failed; nested exception is java.util.concurrent.ExecutionException:
java.lang.IllegalStateException: Iterator is in failed state
Consumer XML
<?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:int="http://www.springframework.org/schema/integration"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd
http://www.springframework.org/schema/integration/kafka http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<int:channel id="inputFromKafka">
<int:queue />
</int:channel>
<int:service-activator input-channel="inputFromKafka"
ref="messageProcessor" method="reader" >
<int:poller fixed-rate="50" task-executor="taskExecutor" >
</int:poller>
</int:service-activator>
<bean id="consumerProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="auto.offset.reset">largest</prop>
<prop key="socket.receive.buffer.bytes">10485760</prop>
<prop key="fetch.message.max.bytes">5242880</prop>
<prop key="auto.commit.interval.ms">100000</prop>
</props>
</property>
</bean>
<int-kafka:zookeeper-connect id="zookeeperConnect"
zk-connect="localhost:2181" zk-connection-timeout="6000"
zk-session-timeout="6000" zk-sync-time="2000" />
<bean id="kafkaThreadListener" class="com.maistylz.adminui.integration.ConsumerStarter"
init-method="initIt" />
<int-kafka:inbound-channel-adapter
kafka-consumer-context-ref="consumerContext" auto-startup="true"
channel="inputFromKafka" id="kafka-inbound-channel-adapter" >
<int:poller fixed-delay="1000" time-unit="MILLISECONDS"
receive-timeout="1000" />
</int-kafka:inbound-channel-adapter>
<task:executor id="taskExecutor" pool-size="50" keep-alive="120" queue-capacity="500"/>
<int-kafka:consumer-context id="consumerContext"
consumer-timeout="400000" zookeeper-connect="zookeeperConnect"
consumer-properties="consumerProperties" >
<int-kafka:consumer-configurations>
<int-kafka:consumer-configuration
group-id="default" max-messages="500" executor="taskExecutor">
<int-kafka:topic id="event-stream" streams="1" />
</int-kafka:consumer-configuration>
</int-kafka:consumer-configurations>
</int-kafka:consumer-context>
</beans>
Producer XML
<?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:int="http://www.springframework.org/schema/integration"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<int:channel id="inputToKafka">
<int:queue/>
</int:channel>
<bean id="producerProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="topic.metadata.refresh.interval.ms">360000000</prop>
<prop key="message.send.max.retries">5</prop>
<prop key="send.buffer.bytes">5242880</prop>
</props>
</property>
</bean>
<bean id="encoder"
class="org.springframework.integration.kafka.serializer.common.StringEncoder" />
<int-kafka:outbound-channel-adapter
id="kafkaOutboundChannelAdapter" auto-startup="true"
kafka-producer-context-ref="kafkaProducerContext"
channel="inputToKafka" >
<int:poller fixed-delay="1000" receive-timeout="0" task-executor="taskExecutor"/>
</int-kafka:outbound-channel-adapter>
<task:executor id="taskExecutor" pool-size="5" keep-alive="1200" queue-capacity="10000"/>
<int-kafka:producer-context id="kafkaProducerContext">
<int-kafka:producer-configurations>
<int-kafka:producer-configuration broker-list="localhost:9092"
topic="event-stream"
key-class-type="java.lang.String"
value-class-type="java.lang.String"
value-encoder="encoder"
key-encoder="encoder"
/>
</int-kafka:producer-configurations>
</int-kafka:producer-context>
</beans>

Spring ACL issue with disk store ehcache

I'm trying to implement Spring ACL, but I encountered an issue to which i have no clue of how to solve.
We have a running configuration of echache which is loaded from the following spring context.
<?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:context="http://www.springframework.org/schema/context"
xmlns:sec="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Support annotation configuration -->
<context:annotation-config />
<context:property-placeholder location="META-INF/test.properties" ignore-unresolvable="true" ignore-resource-not-found="true" />
<!-- Database access -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.iwh.driver}" />
<property name="url" value="${db.iwh.url}" />
<property name="username" value="${db.iwh.username}" />
<property name="password" value="${db.iwh.password}" />
<property name="initialSize" value="${db.iwh.initialConnections}" />
<property name="maxActive" value="${db.iwh.maxConnections}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource"/>
</bean>
<!-- JPA with Hibernate -->
<util:list id="productEntityPackages"/> <!-- Placeholder -->
<util:list id="projectEntityPackages"/> <!-- Placeholder -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="datasource"/>
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
<property name="jpaVendorAdapter">
<bean class="dk.intelligentsystems.platform.util.spring.ISHibernateJpaVendorAdapter">
<property name="databasePlatform" value="${db.iwh.dialect}"/>
<property name="showSql" value="${db.iwh.show_sql}"/>
<property name="hbm2ddl" value=""/>
<!-- <property name="hbm2ddlImportFiles" value="''"/> -->
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.cache.default_cache_concurrency_strategy">read-write</prop>
<prop key="net.sf.ehcache.configurationResourceName">/META-INF/ehcache.xml</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<!--prop key="javax.persistence.sharedCache.mode">DISABLE_SELECTIVE</prop-->
</props>
</property>
<!--property name="packagesToScan" ref="entityManagerPackages"/-->
<!-- Managing transactions explicitly -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="txInterceptor" class="dk.intelligentsystems.platform.tx.ISTransactionInterceptor" />
<aop:config>
<!-- Add custom transaction interceptor to all #Transactional methods -->
<aop:pointcut id="transactionalMethod" expression="#annotation(org.springframework.transaction.annotation.Transactional) || within(#org.springframework.transaction.annotation.Transactional *)"/>
<aop:advisor pointcut-ref="transactionalMethod" advice-ref="txInterceptor" order="1"/>
</aop:config>
<!-- Task scheduling -->
<task:scheduler id="taskScheduler" pool-size="8"/>
</beans>
The ehcache looks as:
<?xml version="1.0" encoding="UTF-8"?>
<!-- See http://ehcache.org/documentation/user-guide/hibernate -->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
maxElementsOnDisk="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
statistics="true"
memoryStoreEvictionPolicy="LRU"/>
<cache name="dk.platform.model.Alarm"
maxElementsInMemory="2000"
eternal="false"
timeToIdleSeconds="360"
timeToLiveSeconds="360">
</cache>
...
The ACL is loaded in this application context (Bottom part stolen from spring security contacts sample):
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="spring-test-base.xml"/>
<sec:global-method-security pre-post-annotations="enabled" secured-annotations="disabled" jsr250-annotations="disabled" proxy-target-class="true">
<sec:expression-handler ref="expressionHandler" />
</sec:global-method-security>
<bean id="expressionHandler"
class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
<property name="permissionEvaluator">
<bean id="permissionevaluator" class="dk.company.util.AuthorizationProvider">
</bean>
</property>
</bean>
<!-- ========= ACL SERVICE DEFINITIONS ========= -->
<bean id="aclCache" class="org.springframework.security.acls.domain.EhCacheBasedAclCache">
<constructor-arg>
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
</property>
<property name="cacheName" value="aclCache"/>
</bean>
</constructor-arg>
</bean>
<bean id="lookupStrategy" class="org.springframework.security.acls.jdbc.BasicLookupStrategy">
<constructor-arg ref="datasource"/>
<constructor-arg ref="aclCache"/>
<constructor-arg>
<bean class="org.springframework.security.acls.domain.AclAuthorizationStrategyImpl">
<constructor-arg>
<bean class="org.springframework.security.core.authority.SimpleGrantedAuthority">
<constructor-arg value="ROLE_ADMINISTRATOR"/>
</bean>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.acls.domain.ConsoleAuditLogger"/>
</constructor-arg>
</bean>
<bean id="aclService" class="org.springframework.security.acls.jdbc.JdbcMutableAclService">
<constructor-arg ref="datasource"/>
<constructor-arg ref="lookupStrategy"/>
<constructor-arg ref="aclCache"/>
</bean>
</beans>
When I try to run the application I get the following exception:
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
// Some thousand stack frames...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.ehcache.EhCacheFactoryBean#51e88ff6' defined in class path resource [META-INF/spring-authorization-test.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: aclCache Cache: Could not create disk store. This CacheManager configuration does not allow creation of DiskStores. If you wish to create DiskStores, please configure a diskStore path.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:271)
... 45 more
Caused by: net.sf.ehcache.CacheException: aclCache Cache: Could not create disk store. This CacheManager configuration does not allow creation of DiskStores. If you wish to create DiskStores, please configure a diskStore path.
at net.sf.ehcache.store.compound.factories.DiskOverflowStorageFactory.getDataFile(DiskOverflowStorageFactory.java:79)
at net.sf.ehcache.store.compound.factories.DiskOverflowStorageFactory.<init>(DiskOverflowStorageFactory.java:71)
at net.sf.ehcache.store.compound.impl.OverflowToDiskStore.create(OverflowToDiskStore.java:63)
at net.sf.ehcache.Cache.initialise(Cache.java:1113)
at net.sf.ehcache.CacheManager.addCacheNoCheck(CacheManager.java:1081)
at net.sf.ehcache.CacheManager.addCache(CacheManager.java:987)
at org.springframework.cache.ehcache.EhCacheFactoryBean.afterPropertiesSet(EhCacheFactoryBean.java:333)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
... 48 more
I tried removing the <diskStore path="java.io.tmpdir"/> line in the ehcache, but to no avail.
EDIT
Spring version: 3.2.4.RELEASE
Ehcache version: 4.1.9.Final
According to EhCache documentation, you can turn off usage of disk for a cache by setting the overflowToDisk attribute to false. So you could try :
<defaultCache
maxElementsInMemory="10000"
overflowToDisk="false"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskExpiryThreadIntervalSeconds="120"
statistics="true"
memoryStoreEvictionPolicy="LRU"/>

Integrating JTA, Hibernate 4 and Spring 4 with WebLogic

I am having serious issues with implementing a JTA / Hibernate / Spring application (with WebLogic 10.3 for the server). I originally was using the Spring programmatic configuration, but have moved it over to the Spring XML configuration to filter out potential problems. It was working perfectly when I used the HibernateTransactionManager instead of the JtaTransactionManager, but now is not finding the Session for the thread.
When I look at the sessionFactory it is not null, but getSession() does not return a session.
Could you assist me in troubleshooting this? Thank you!
My Dao implementation:
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
...
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import mil.navy.navsupbsc.utilities.HibernateConfiguration;
#Repository
public class ContactDAOImpl implements ContactDAO {
#Autowired
SessionFactory sessionFactory;
public Session getSession() {
try {
sessionFactory.openSession();
return sessionFactory.getCurrentSession();
} catch (NullPointerException e) {
e.printStackTrace();
throw e;
}
}
#SuppressWarnings("unchecked")
public List<Contact> listContact() {
List<Contact> contactList = getSession().createCriteria(Contact.class)
.list();
...
My Spring-Servlet.xml file is:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="mil.navy.navsupbsc.entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.driverClassName">oracle.jdbc.OracleDriver</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="mil.navy.navsupbsc" />
<tx:annotation-driven />
<jee:jndi-lookup id="NCS" jndi-name="NCS" resource-ref="false"></jee:jndi-lookup>
<tx:jta-transaction-manager />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
My web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" metadata-complete="false" version="2.5">
<display-name>Contact Management Suite</display-name>
<welcome-file-list>
<welcome-file>contact</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<resource-ref>
<description>NCS</description>
<res-ref-name>NCS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
And my weblogic.xml file
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.2/weblogic-web-app.xsd">
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.hibernate.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
<wls:weblogic-version>10.3.5</wls:weblogic-version>
<wls:context-root>ncms2</wls:context-root>
<wls:resource-description>
<wls:res-ref-name>dbDataSource</wls:res-ref-name>
<wls:jndi-name>NCS</wls:jndi-name>
</wls:resource-description>
</wls:weblogic-web-app>
Based on the answer from #M. Deinum (if you put your comments in an answer, I'll mark it as correct), I have figured out the problem.
The solution was to add the data source (referencing the JNDI data source I specified earlier) as <property name="dataSource" ref="NCS"/>. I also reference the jtaTransactionManager as <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform</prop>
NOTE: for other servers other than WebLogic, replace
.WebLogicJtaPlatform with your specific server's JtaPlatform name.
Specific implementations can be found at
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/service/jta/platform/internal/AbstractJtaPlatform.html
Finally, I changed the sessionFactory.openSession() to sessionFactory.getSession in my DaoImplementation file.
I hope this helps someone else as well!
Spring-Servlet.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="packagesToScan" value="[app.package].navsupbsc.entity" />
<property name="dataSource" ref="NCS"/>
<property name="jtaTransactionManager" ref="transactionManager" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.WeblogicJtaPlatform
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.driverClassName">oracle.jdbc.OracleDriver</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
</bean>
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<context:component-scan base-package="[app.package.scanpath]" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jee:jndi-lookup id="NCS" jndi-name="NCS" resource-ref="false">
</jee:jndi-lookup>
<tx:jta-transaction-manager id="transactionManager" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
</beans>
Sources:
http://koenserneels.blogspot.de/2012/05/migrating-from-hibernate-3-to-4-with.html
http://devblog.x-sphere.com/2008/01/23/websphere-jndi-spring-framework-hibernate/ (NOTE: I'm using <tx:jta-transaction-manager/> in place of the manual transaction manager shown in the link

Spring + JPA in standalone application doesn't save data in database

I am using
Spring 3.1.1
JPA 2
H2
Hibernate
in standalone Java application. When I run the application, the transaction is executed and when I query the data, the data is retrieved. But the data is not saved when the application is closed.
Please help me.
Here is spring configuration,
<?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"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml" />
</bean>
<bean id="sharedEntityManager"
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven />
</beans>
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="sling">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.sling.data.Trend</class>
<class>com.sling.data.Gc</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.connection.driver_class" value="org.h2.Driver" />
<property name="hibernate.connection.url" value="jdbc:h2:sling;DB_CLOSE_DELAY=-1" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
</properties>
</persistence-unit>
</persistence>
DAO class,
#Repository
public class GcDao {
#PersistenceContext
private EntityManager em;
public GcDao() {
}
#Transactional
public void add(Gc gc){
em.persist(gc);
}
#SuppressWarnings("unchecked")
#Transactional(readOnly = true)
public List<Gc> getGc(){
String queryText = " from Gc";
Query query = em.createQuery(queryText);
return query.getResultList();
}
}
I believe you still need to add context:component-scan on your dao package before your tx:anotation-driven element.
<context:component-scan base-package="com.sling.dao" />
<tx:annotation-driven />

Resources