Spring Error: org.springframework.beans.factory.BeanCreationException - spring

I have a web application which I am exporting as EAR and trying to deploy on Websphere 6.1 application server.
The deployment goes fine. Even, the index page loads fine which is a login screen.
However, on entering credentials and hitting enter, the next page does not load and HTTP 500 Internal Server Error is thrown.
The next page which gets loaded is a JSP having a country drop down, whose values gets fetched from database via hibernate.
I have Datasource created in Websphere with all details and even test connection succeeds.
However, I get the below error on checking the server System.out logs:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ds':
Invocation of init method failed; nested exception is javax.naming.NameNotFoundException:
Context: uschwasvmdev04Cell01/clusters/URMDUS, name: jdbc/mydbXA: First component in name
mydbXA not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound:
IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
The description of NameNotFoundException says:
"This exception is thrown when a component of the name cannot be resolved because it is not bound. "
JNDI Lookup in my services.xml also seems fine:
<jee:jndi-lookup id="ds" jndi-name="jdbc/mydbXA" resource-ref="true" />
What am I missing here ?
Thanks for reading!

You are using resource-ref="true". Have you created the according resource-reference in your web.xml and did you map the DataSource to the application?
If you set it to false you can do a global lookup. Otherwise you have to configure it.

It's funny, but I've done the opposite: I created the resource-reference but I forget to tell Spring to use it.
Just in case, in the namespace it's done as you can see above: resource-ref="true"
If you're configuring your beans manually:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/database" />
<property name="resourceRef" value="true" /><!-- It's false by default -->
</bean>

Related

Spring Batch & Azure SQL Server: Could not get JDBC Connection

We are in the process of moving to Azure SQL Server from Oracle DB for our Spring Batch application.
I am getting the following error intermittently
ERROR : 01.03.2022:1458 (40.269) [[]main] CommandLineJobRunner: Job
Terminated in error: Error creating bean with name 'dateStoreList':
Cannot resolve reference to bean 'jobRepository' while setting bean
property 'jobRepository'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'jobRepository': Invocation of init method
failed; nested exception is
org.springframework.jdbc.support.MetaDataAccessException: Could not
get Connection for extracting meta data; nested exception is
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not
get JDBC Connection; nested exception is
org.apache.commons.dbcp.SQLNestedException: Cannot create
PoolableConnectionFactory (The TCP/IP connection to the host
sqlsrv-01.database.windows.net, port 1433 has failed. Error:
"sqlsrv-01.database.windows.net. Verify the connection properties.
Make sure that an instance of SQL Server is running on the host and
accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.".)
Data Source:
<!-- Connection Pooled DATA SOURCE -->
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
<propertyname="driverClassName"value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="connectionProperties"value="sendStringParametersAsUnicode=false;"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
<property name="initialSize" value="2" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="1" />
<property name="validationQuery" value="SELECT 1"/>
<propertyname="testOnBorrow"value="false"/>
<propertyname="testWhileIdle"value="true"/>
<propertyname="timeBetweenEvictionRunsMillis"value="1200000"/>
<propertyname="minEvictableIdleTimeMillis"value="3000000"/>
<propertyname="numTestsPerEvictionRun"value="10"/>
<propertyname="poolPreparedStatements"value="true"/>
<propertyname="defaultAutoCommit"value="false"/>
</bean>
How do I enable retry at the spring batch application level to handle any database intermittent failure and recovery automatically?
According to this StackOverflow answer, as a temporary solution, you can add ?autoReconnect=true to your JDBC URL. I wonder, though, if this is a bandaid solution, and if there is a worse issue present. Take a look at this Microsoft article about dropped connections. Are your connections getting stale and being dropped?

Grails - RabbitMQ and listener - too more onMessage call

At first I have application where I'm using RabbitMQ and Grails.
I defined listeners:
<bean id="rabbitListenerCreateDocument" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="queues" ref="rabbitQueuePageInstanceCreated" />
<property name="defaultRequeueRejected" value="false"/>
<property name="messageListener" ref="createPageService" />
<property name="errorHandler" ref="errorHandlerService" />
<property name="transactionManager" ref="mongoTransactionManager"/>
<property name="autoStartup" value="true" />
<property name="concurrentConsumers" value="0" />
</bean>
The problem is that onMessage method from createPageService run too more times.
I think because I got this error (when I turn onlogs from spring):
connection error; reason: com.rabbitmq.client.impl.UnknownChannelException: Unknown channel number 10
After some time I got that spring dosnt send ack to rabbit when this exception was thrown.
Everything should look fine, but...
In'm method onMessage I'm creating document in MongoDB - and I'm geting too much documents - it CRITICAL in my app.
I'm using Mongo to store some objects, and as we all know MongoDB is not transactional.
I think that scenario looks like this:
Listener getting message
method onMessage from createPageService is running
method creating document in Mongo
method onMessage reach to end
Spring want to send ACK to rabbit but it get UnknownChannelException
TransactionManager want to rollback but it can't - there is no transaction on MongoDB
The same message came one more time and now everything works ok (there is no Exception)
I trying to make some modification to solve this but it doesn’t works:
1. First I'm trying to add Around aspect on onMessage method - it dosnt works ... I'm getting:
Error creating bean with name 'grailsApplicationPostProcessor': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
I define new transaction manager for connection for bean: createPageService. It not work's too..
Somebody have any idea to solve this problem ?
I've been using this plugin for grails without issues:
https://github.com/pablomolnar/rabbit-ha
It has some extra configuration, but for basics it should be enough.

Spring BeanCreationException: cannot resolve reference to bean exception

I am trying the example application at the following web site:
JSF 2, PrimeFaces 3, Spring 3 & Hibernate 4 Integration Project
But I find that when running the project, I get:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]
However, in the applicationContext.xml file, the relevant code is as follows:
<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>
<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
<property name="userDAO" ref="UserDAO" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.otv.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
The classes do exist in the relevant packages as well as can be seen below and the location of the various config files.
The only difference I can see between the tutorial and my implementation of it is that I am using NetBeans 7.2 rather than Eclipse.
Has anyone any idea as to why this is?
/WEB-INF/applicationContext.xml should contain an entry like <bean id="UserDAO" class="com.otv.dao.UserDAO">...</bean> whose properties largely depend on the backend system used.
I also suspect that the User bean is a bad copy and past as User instances should be retrived from the DAO or created programmatically.
As to why it works in Eclipse and not in Netbeans, it is too strange to be true. There must be some clutter...
I found the main cause of that error. It is actually quit simple.
In the class com.otv.model.User, there is no #Id annotation above the field id.
Here is the link for the answer that lead me to find what was the mistake : hibernate exception: org.hibernate.AnnotationException: No identifier specified for entity: com..domain.idea.MAE_MFEView
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO';
This is telling you that UserService can't be created because its missing a property definition
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]
This tells you that the definition for UserDAO can't be found.
You are missing the UserDao definition, the ref just means that it should be of that type, it still needs a bean definition.
Basically, whenever you use "ref" you are telling spring to make a property of that type. That type needs to be defined in its own bean definition.
So if UserDao uses some other property that again is defined by a "ref" that property will need its own bean definition as well.
You have to think of the classes and the spring definitions to be two completely separate entities. The classes might be there and placed where they should, but spring needs its bean definitions in order to invoke them. It doesn't know what a UserDao or SessionFactory is unless you specifically tell it which package/class you want to be invoked.

Can't seem to get a JNDI JDBC resource working in Liferay

No joy in the Liferay forum on this issue and the clock is ticking on this project. This may be caused by my lack of knowledge of Spring.
I have a JNDI global resource defined in server.xml and a resource link in context.xml in my Tomcat 7 /conf folder. I KNOW the JNDI resource is being loaded because I see the validation query being run as the server starts up. So far so good.
I have a portlet that just provides services to other portlets. In that portlet I have a hibernate.cfg.xml which has a session-factory that also points to the JDBC resource (don't know if this is needed or not). I also have an ext-spring.xml file in the services portlet that has the following:
<bean id="liferayHibernateSessionFactory" class="com.liferay.portal.spring.hibernate.PortletHibernateConfiguration" >
<property name="dataSource" ref="MyJDBCResource" />
</bean>
<bean id="MyJDBCResource" class="org.springframework.jndi.JndiObjectFactoryBean" >
<property name="jndiName" value="java:comp/env/jdbc/MyJDBCResource" />
</bean>
Adding the above in ext-spring.xml fixed an issue with a bean error on that services portlet upon deployment. In that service builder built portlet, a services jar was created and I put that service jar in the Tomcat_Home/lib/ext folder so that I could use the services provided by the portlet in my portlet. So far so good. But, when I invoke the portlet method which calls the services provided by the other portlet with the JNDI references, I get a "user lacks privilege or object not found" error. It is definitely object not found. When the query is run I see absolutely NO activity on the JDBC connection specified by the JNDI resource entry and in drilling down on the connection properties I only see the HSQLDB driver in use. It should be using the MSSQL driver specified in my global resource JNDI entry as far as I understand it.
SO WHAT AM I DOING WRONG? Do I need to add some configuration entries in the portlet that invokes the services?
This seems so simple. In reading the many posts that give instructions on using JNDI/JDBC resources I seem to have followed them correctly. Is there some trick to using JNDI/JDBC resources in LR 6.1.1 and Tomcat 7 that I have missed?
Thanks (and really hoping for some answers!).
First, you could try rewrite JNDI resource reference like this:
<bean id="MyJDBCResource" class="org.springframework.jndi.JndiObjectFactoryBean" >
<property name="jndiName" value="jdbc/MyJDBCResource" />
</bean>
also, you could try different approach on JNDI resource lookup in Spring:
<jee:jndi-lookup id="MyJDBCResource" jndi-name="jdbc/MyJDBCResource" expected-type="javax.sql.DataSource" />
Not sure about first approch, but second will definitively fail early in case no JNDI resource could be found.
Hope this helps.

Remote EJB3 access from Spring

I have 2 applications - one EAR containing EJB3.0 and other its Spring client. Both deployed on same Websphere app server 7 instance i.e. same JVM.
Here are couple of client parts:
applicationContext.xml:
<bean id="ruleService" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="jndiName" value="com.ecrd.ruleservice.Customer_Management_Service_JavaBusiness"/>
<property name="businessInterface" value="com.ecrd.ruleservice.Customer_Management_Service_JavaBusiness"/>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
<prop key="java.naming.provider.url">corbaloc:iiop:localhost:2809</prop>
</props>
</property>
</bean>
Client code snippet:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Customer_Management_Service_JavaBusiness ruleService
= (Customer_Management_Service_JavaBusiness)ctx.getBean("ruleService");
This works fine. EJB access deployed in other application in same container, works.
But it does not work if I move that client to other machine in same network and just change provider URL and give remote server IP instead of localhost. JNDI lookup problem occurs
Exact Error:
Error 500: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ruleService' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Context: CFSDDVM2399Node01Cell/nodes/CFSDDVM2399Node01/servers/server1, name: com.ecrd.ruleservice.Customer_Management_Service_JavaBusiness: First component in name com.ecrd.ruleservice.Customer_Management_Service_JavaBusiness not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]
I can't figure out what changes (and where) are required to connect to EJB hosted in different server JVM. Any help or suggestion are welcome.
Answering myself. Error is resolved.
I work on virtual machine. All developer images were copied from a particular VM and that
brought over WAS configs (hostname, cell, node etc) with it. Remote access
worked after fixing all occurances of hostnames in serverindex.xml of the server
hosting EJB.

Resources