ActiveMQ in JBoss - Using JNDI for jdbcPersistentAdaptetr - oracle

Running ActiveMQ in clustered environment with a Master/Slave relationship using Oracle as a datastore. Using a jdbcPersistentAdapter.
<jdbcPersistenceAdapter dataSource="#dataSource" createTablesOnStartup="false" lockKeepAlivePeriod="30000"/>
The activemq broker is running embedded in JBoss.
I would like to replace the dataSource bean (contains credentials and url string) with a JNDI reference since that already manages the database connection. Is this possible?

Load the dataSourcebean from JNDI instead. Something like this should work (using a correct JNDI name from your setup).
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/MyDatabase"/>
</bean>

Related

Spring application should start even database is not available at startup

I have an old spring application which uses jee:jndi-lookup for datasource. This application running on Tomcat 8.
<jee:jndi-lookup id="datasource" jndi-name="java:/comp/env/jdbc/Tomcat8Database" destroy-method="close" expected-type="javax.sql.DataSource" lookup-on-startup="false"/>
The database may be sometime down at startup of the application, but as I also tried to lazy-init spring beans it did not helped as what it seems like that JNDI lookup in spring happened on Startup always or its not in spring controls as Server provide Pooling over connections.
Any idea or code example will be helpful.
According to spring javadoc, For a lazy lookup, a proxy interface needs to be specified.
Proxy interface specify the proxy interface to use for the JNDI object.
Typically used in conjunction with "lookupOnStartup"=false and/or "cache"=false. Needs to be specified because the actual JNDI object type is not known in advance in case of a lazy lookup.
Try:
<jee:jndi-lookup id="datasource" jndi-name="java:/comp/env/jdbc/Tomcat8Database" destroy-method="close" expected-type="javax.sql.DataSource" lookup-on-startup="false" proxy-interface="javax.sql.DataSource"/>

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>

spring batch use data source instead of jdbc using batch-db2.properties

I am fairly new to spring batch. I have it working embedded inside my spring mvc web application. It is using a db2 database data store to log job information. At present my batch-db2.properties reads as...
batch.jdbc.driver=com.ibm.db2.jcc.DB2Driver
batch.jdbc.url=<<my database url>>
batch.jdbc.user=<<my database user>>
batch.jdbc.password=<<my database password>>
batch.schema=<<my database scehma>>
batch.jndi.name=jdbc/<<my jndi url>>
So I've set both jdbc properties and well as the jndi property. The jobs are running fine but my question is what type of connection is my spring batch installation using.
If both are set does it use jdbc or jndi? Also can someone point me to the spring batch documentation page where it gives more information about these settings. I could not find it.
Here is my data source configuration....
<beans profile="server">
<bean id="ePosDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="java:comp/env/jdbc/reportingManagerDataSource"
p:lookupOnStartup="false"
p:cache="true"
p:proxyInterface="javax.sql.DataSource"/>
<jee:jndi-lookup id="ePosDataSource"
jndi-name="jdbc/reportingManagerDataSource"
cache="true"
resource-ref="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
<bean id="custDbDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean"
p:jndiName="java:comp/env/jdbc/reportingManagerCustDbDataSource"
p:lookupOnStartup="false"
p:cache="true"
p:proxyInterface="javax.sql.DataSource" />
<jee:jndi-lookup id="custDbDataSource"
jndi-name="jdbc/reportingManagerCustDbDataSource"
cache="true"
resource-ref="true"
lookup-on-startup="false"
proxy-interface="javax.sql.DataSource"/>
</beans>
thanks!

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

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 ")?

Spring disconnecting from Derby

I'm using Apache Derby with the Spring JdbcTemplate inside a web app running on Tomcat.
Spring is managing the data source. I've noticed that if I update the .war file and Tomcat undeploys/redeploys the app, I get this error:
java.sql.SQLException: Another instance of Derby may have already booted the database /tmp/manager_db/manager.
Restarting Tomcat fixes the problem, but as a purist, I'd like to clean things up properly when the webapp is undeployed.
The Embedded driver doesn't seem to have a 'close' method to put in the bean declaration under 'destroy-method'. I know the shutdown is normally achieved using a 'shutdown' connection URL, "jdbc:derby:;shutdown=true".
Any suggestions?
Here's the declaration in the Spring config file for my data source (the db won't be under /tmp/, just there for now).
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:/tmp/manager_db/manager;create=true"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
I think a better answer is to use the Tomcat JNDI data source pool with Spring. Spring's JDBC template will return the connection to pool when it's done.

Resources