Exception Handling in Spring Framework JNDI - spring

I have configured JNDI reference in spring-context.xml ,created JNDI in Websphere application server 7.5, this working fine, but if its database is down, I am not able to start the web application ,i am getting 500 uncaught servlet initialization exception .
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${reports_db_jndi_ref}"/>
</bean>
Could you please advise ? How to handle the exception or how to start the web application even though the database is down?

Set the lookupOnStartup property to false so that Spring returns a proxy to the datasource instead of the actual datasource. However if your application uses the datasource as part of the startup process e.g due to some dependency trying to connect to the database still the looup will occur. Change as follows
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${reports_db_jndi_ref}"/>
<property name="lookupOnStartup" value="false"/>
</bean>

Related

Websphere RASWsLoggerFactory needed for Hibernate 4.3?

I'm trying to use Hibernate 4.3 in my existing spring 4.3 web-mvc app.
I have included the jars which are required from the distribution list.
Beans in my application context.
<orcl:pooling-datasource id="dataSource" connection-properties-prefix="conn" properties-location="WEB-INF/orcl.properties"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
While creating the sessionFactorybean, the spring bean is trying to resolve jtaplatform and expects Websphere app server specific logger class.
But the fun part i'm running my app on tomcat, which does not have those bootstrap.jar or the classes of websphere.
My question is why do we need Websphere jars, i went inside StandardJtaPlatformResolverand found that the last possible(if else or a try catch method) jta provider would be websphere.
Not sure why should i provide jta provider in this case ?
Below is the actual error out.
Finally found the culprit. Long back this app was running on websphere server, which was then migrated to jboss.
This unused
jar has misled hibernate to think that there is websphere jtaplatform available.
So, removing this junk jar solved it.

Strange error occured in shiro doGetAuthorizationInfo method

I am encountered with a strange error for a few days,and still in puzzled,details are below:
I'm using apache shiro with spring,
after some work,I got ready to setup "doGetAuthorizationInfo" method,because I'm using "#RequiresRoles" in my controller,
I found if I invoke XXXService(or any service) more than once,the exception occur(first time invoke XXXService,everything work fine),so,I'm try to test "doGetAuthenticationInfo" in same Realm which used for login,I invoked XXXService or other services several times in the method,it works fine,
and I also tried to change different datasource component,
so I think it's not datasource component bug,
I didn't find the reason.
When I was using dbcp datasource,exception like below:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
No others are causing exception.
Any other place didn't found this problem.
Thanks for any help.
Through the efforts more than one day,I find something,
I was using dataSourceProxy:
<bean id="dataSourceProxy"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<property name="targetDataSource" ref="dataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceProxy" />
...
</bean>
When I change to ref original datasource bean for entityManagerFactory:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
...
</bean>
The error disappear.

For camel JmsConfiguration Spring cachingconnectionfactory can be used with JndiObjectFactoryBean inside websphere Appserver

My application is running under Websphere Application server and uses apache camel for message routing. My QueueConnectionFactories and Queues are created as JMS resources in the Appserver with jndi pointing to WebsphereMQ Queue Manager. Can I use SpringCachingConnectionFactory for better performance inside JEE container like WAS with JNDI. Following spring config:
<bean id="jmsDestinationResolver" class="org.springframework.jms.support.destination.JndiDestinationResolver">
</bean>
<bean id="myJndiObjectFacory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jms/MyQCF"/>
</bean>
<bean id="myJmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
<property name="connectionFactory" ref="myJndiObjectFacory"/>
<property name="transactionManager" ref="txManager"/>
<property name="destinationResolver" ref="jmsDestinationResolver"/>
Will above code have same performance as of spring CachingConnectionFactory ?
or
Is it possible to use CachingConnectionFactory with Jndi lookup in websphere JEE container?

Setup of JMS message listener invoker failed for destination 'queue:XYZ:No JTA UserTransaction available

We are upgrading our project from Spring 2.5.6 to 3.2.3 and Hibernate/JPA to 4.2.3.
In spring-ds.xml for transaction management we replaced original below config
<bean id="transactionManager"
class="org.springframework.transaction.jta.WebSphereUowTransactionManager">
<!-- This property is specifically required for JMS -->
<property name="transactionManager" ref="baseTransactionManager" />
</bean>
<bean id="baseTransactionManager"
class="org.springframework.transaction.jta.WebSphereTransactionManagerFactoryBean" />
<tx:annotation-driven transaction-manager="transactionManager" />
to below as WebSphereTransactionManagerFactoryBean class is superseded in latest WAS :
<bean id="transactionManager"
class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
and JMS msg listener config looks like below :
<bean id="xxtMsgListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsxxConnectionFactory" />
<property name="destination" ref="jmsxxQueue" />
<property name="messageListener" ref="xxMessageListener" />
<property name="transactionManager" ref="transactionManager" />
<property name="taskExecutor" ref="taskExecutor" />
</bean>
With above config we are getting below error in WAS logs :
Setup of JMS message listener invoker failed for destination
queue://xxQueue?busName=zzBus' - trying to recover. Cause: No JTA UserTransactionavailable - programmatic PlatformTransactionManager.getTransaction usage not supported
Is there any other config/property required to upgrade to spring 3.2.3 ? or to config WebSphereUowTransactionManager do we need to set any property ?
In case you are using Hibernate in your application, the actual Hibernate version used can be the root cause of the problem.
We spent half a day debugging it (on a WebSphere box), and then found that indeed it was the hibernate version upgrade (from 4.2.7.Final to 4.2.12.Final) which caused issue, not the JMS configuration.
UPDATE: It seems that Hibernate includes transaction-api jboss-transaction-api_1.1_spec which was not compatible with the one present on Websphere. Simply excluding this from hibernate resolved the issue.
on the DefaultMessageListenerContainer, try setting the sessionTransacted property to true. this should enable transaction support with WebSphere
The error happens because you have used JTA transaction manager, while your connection factory is not XA capable. Essentially injected implementation of ConnectionFactory does not implement JTA interfaces. Thus transaction manager isn't able of enrolling a message consumption into a new instance of UserTransaction.
In other to fix this issues the one needs to use XA capable ConnectionFactory, or other non-jta transaction manager like Spring's JmsTransactionManager.

Initializing a sessionfactory after web application startup

in common, hibernate sessionfactory is created in spring configuration file (eg spring-dao.xml) like;
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>
and then in dao,
<bean id="myProductDao" class="product.ProductDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
in web.xml, we put the config file (spring-dao.xml) in contextConfigLocation;
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-dao.xml</param-value>
</context-param>
when the application is started, datasource is injected to all dao beans.
This was the summary, what my problem is, I do not want spring to connect to database on application startup. I have an admin(responsible for opening db connection after startup) and an admin applet working on remote machine, which communicates with web app servlet. database connection for web application should be opened if authentication is ok.
how can i achieve this goal?
Specify lazy-init="true" on the mySessionFactory bean and it will be initialized when your code tries to access it for the first time i.e. when the authentication is successful.
The solution was not to hard; Create a datasource with no parameters initially, and then setting values after admin credentials ok.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- Connection properties. All should be ommitted. -->
</bean>
// code below is called after admin login
DataSource dS = context.getBean("dataSource");
dS.setUrl("...");
dS.setUserPass(adminPass);

Resources