Spring : Tomcat datasource via JNDI in my Spring configuration Problem? - spring

I want to read Tomcat datasource via JNDI in my Spring configuration i am using oracle toplink
in spring applicationContext.xml i am using like below
<bean id="UserDatabase" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/ISM_rep_user"></property>
<property name="lookupOn" value="true"></property></bean>
and in tomcat/conf/context.xml i am using below
Thanks,

Maybe the lookupOn property has no vaid XML syntax (missing ")?

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.

Exception Handling in Spring Framework JNDI

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>

setting up JDBCTemplate in Spring using java configuration file

I read in Spring in Action that a good way to set up JDBCTemplate is adding this in the Spring config file:
<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/AOICMainDB" expected-type="javax.sql.DataSource" />
<bean id="jdbcTemplateDB2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dbDataSource" />
</bean>
it makes sense, now we can autowire jdbcTemplateDB2 in a DAO and do jdbcTemplate stuff with it.
but how would I set this up using a java config file? Specifically I'm not sure how the jee: namespace translates over to java confg.
The jee:jndi-lookup is just syntactical sugar for the JndiObjectFactoryBean.
The spring documentation provides a before and after example like so:
Before…​
<bean id="simple" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/MyDataSource"/>
</bean>
After…​
<jee:jndi-lookup id="simple" jndi-name="jdbc/MyDataSource">
</jee:jndi-lookup>
Now to convert this into a java config you will need to do something like this:
#Bean
public JndiObjectFactoryBean simple() {
JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
bean.setJndiName("jdbc/MyDataSource");
return bean;
}
Then you can just retrieve the jndi object from the bean.
References:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/xsd-configuration.html#xsd-config-body-schemas-jee-jndi-lookup-environment-single
Disclaimer: The public void afterPropertiesSet() must run to populate the jndi object.
Your question should be: Who is running the JNDI service that will give the data source to my app?
If your app doesn't run on a Java EE app server with a JNDI service available, the answer is "No one." You should use a DriverManager data source in that case.
If your app does run on a Java EE app server with a JNDI service available, you have to know how to set up your data source in the pool.

Create DataSource using JBoss 7 JNDI and Spring

I am first time making a webapp for Jboss server.
For JBoss we have the jndi details, but I am wondering how to create the Datasource using it in spring application context.
If anyone has an example to create the connection, please share it.
I will put this sample here.
just to show another way to do it.
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:jboss/datasources/DSName</value>
</property>
</bean>
I found the solution
Add below configuration to applicationContext.xml
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
<jee:jndi-lookup expected-type="javax.sql.DataSource" id="dataSource" jndi-name="java:jboss/SAMPLE_JNDI"/>

Configuring Datasource in Spring 3.0

Hello guys I have configured a connection pool and JNDI resource in glassfish 2.1. I can get the Datasource via lookup method in my projects and everything works good. However I decided to try Spring framework and to use my existing connection pool.
In the Spring context file I have the following:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="simpleJdbcTemplate" ref="jdbcTemplate"/>
</bean>
When I deploy the project I get:
java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required]
Is there anything else I have to configure in that file or in any other file in order to get the Datasource?
Presumably, com.mycompany.mavenproject3.Dao extends JdbcDaoSupport, but you're setting a property named simpleJdbcTemplate on it, leading me to believe that you've defined your own property to hold the template since that doesn't exist on Spring's implementation. It's therefore complaining at you because you're required to set either the dataSource property or the jdbcTemplate property of the JdbcDaoSupport object before using it, exactly like it's telling you. Change <property name="simpleJdbcTemplate"... to <property name="jdbcTemplate"....
If your DAO doesn't extend JdbcDaoSupport, then find what does and remove it or set its properties appropriately.
You can also call your datasource directly in your dao bean, don't need to do an another bean for jdbcTemplate. So your context file become something like this:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="dataSource" ref="dataSource"/>
</bean>
After you just have to extends JdbcDaoSupport spring class (in which contain the getter and setter of datasource) on your Dao class.

Resources