Check Datasource connection during initialization - java

I am using org.apache.commons.dbcp.BasicDataSource and com.mchange.v2.c3p0.ComboPooledDataSource APIs to manage datasource connections.
When I have changed the correct connection parameters I was expecting these API gave an exception during initialization but they did not. What am I missing?
<bean id="uslDataSource1" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${usl.db.driverClass}"/>
<property name="url" value="${usl.db.jdbcUrl}"/>
<property name="username" value="${usl.db.username}"/>
<property name="password" value="${usl.db.password}"/>
<property name="initialSize" value="${usl.db.initialPoolSize}"/>
<property name="validationQuery" value="select 1 from dualx"/>
<property name="testOnBorrow" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1200000"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="5"/>
<property name="defaultAutoCommit" value="true"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="scpcDataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${scpc.db.driverClass}" />
<property name="jdbcUrl" value="${scpc.db.jdbcUrl}" />
<property name="user" value="${scpc.db.username}" />
<property name="password" value="${scpc.db.password}" />
<property name="initialPoolSize" value="${scpc.db.initialPoolSize}" />
<property name="minPoolSize" value="${scpc.db.minPoolSize}" />
<property name="maxPoolSize" value="${scpc.db.maxPoolSize}" />
<property name="acquireIncrement" value="${scpc.db.acquireIncrement}" />
<property name="autoCommitOnClose" value="${scpc.db.autoCommitOnClose}" />
<!-- property name="maxIdleTime" value="${scpc.db.maxIdleTime}" / -->
<property name="idleConnectionTestPeriod" value="${scpc.db.idleConnectionTestPeriod}" />
<property name="preferredTestQuery" value="${scpc.db.validationQuery}" />
<property name="maxStatements" value="${scpc.db.maxStatements}" />
</bean>

Related

How to setup multiple data sources with Spring and jdbcTemplate with XML

I'm setting up two different datasource for diff databse in spring-servlet.xml
I have tried multiple solutions provided on sites most of them are for spring-boot.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSourceDev"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="jdbcTemplateDev" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSourceDev" />
</bean>
In java class -
#Autowired
#Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;
#Autowired
#Qualifier("jdbcTemplateDev")
private JdbcTemplate jdbcTemplatedev;*/

add redeliveryPolicy to Websphere MQ message

I have a Websphere MQ and a java app receiveng messages from it. I want to make redelivering system if any exceptions is thrown .
Is there a way to add redeliveryDelay in my configuration spring xml?
here's my spring configuration:
<!-- JMS CONNECTION FACTORY -->
<bean id="MQFactory" class="com.ibm.mq.jms.MQConnectionFactory">
<property name="transportType">
<util:constant static-field="com.ibm.msg.client.wmq.WMQConstants.WMQ_CM_CLIENT" />
</property>
<property name="queueManager" value="${queueManager}" />
<property name="hostName" value="${hostName}" />
<property name="port" value="${port}" />
<property name="channel" value="${channel}" />
</bean>
<bean id="JmsConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory" ref="MQFactory" />
<property name="username" value="${username}" />
<property name="password" value="${username}" />
</bean>
<!-- JMS LISTENER -->
<bean id="Listener" class="jms.impl.Listener"></bean>
<!-- JMS CONTAINER -->
<bean id="JmsContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="JmsConnectionFactory" />
<property name="destinationName" value="${destination}" />
<property name="messageListener" ref="Listener" />
<property name="autoStartup" value="false" />
<property name="concurrentConsumers" value="${jms.consumers}" />
<property name="sessionTransacted" value="true" />
</bean>

Server stops to respond after using spring transaction management

I configured spring transaction management, and it is working as expected. However, when the load increases the server stops responding.
I am working on a multi module project that I have configured in database.xml, which is inside the DAO module
Screenshot of directory structure of my project:
<?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:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="za.co.virginactive.dao" />
<context:property-placeholder location="classpath:jdbc.properties"/>
<tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clubno from club"/>
</bean>
<bean id="imageDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${image.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${image.jdbc.url}" />
<property name="user" value="${image.jdbc.username}" />
<property name="password" value="${image.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clientno from images"/>
</bean>
<bean id="mdsDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${mds.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${mds.jdbc.url}" />
<property name="user" value="${mds.jdbc.username}" />
<property name="password" value="${mds.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT top 1 id from mdm.club"/>
</bean>
<bean id="accessDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${access.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${access.jdbc.url}" />
<property name="user" value="${access.jdbc.username}" />
<property name="password" value="${access.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 clubno from club"/>
</bean>
<bean id="archiveDS" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${archive.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${archive.jdbc.url}" />
<property name="user" value="${archive.jdbc.username}" />
<property name="password" value="${archive.jdbc.password}" />
<property name="testConnectionOnCheckout" value="true"/>
<property name="testConnectionOnCheckin" value="true"/>
<property name="preferredTestQuery" value="SELECT first 1 * from rdb$database"/>
</bean>
<!--<jdbc:initialize-database data-source="dataSource">-->
<!--<jdbc:script location="classpath:ddl/db-schema.sql"/>-->
<!--</jdbc:initialize-database>-->
</beans>

The configuration for com.mchange.v2.c3p0.ComboPooledDataSource

I am looking for help with configuration of my data source type com.mchange.v2.c3p0.ComboPooledDataSource
I need to have 1000 database connections available concurrently.
I started with following configuration and tried to increase the number for initialPoolSize, maxPoolSize, minPoolSize but it doesn’t work.
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="someUrl"/>
<property name="user" value="someUser"/>
<property name="password" value="somePass"/>
<property name="acquireIncrement" value="1" />
<property name="acquireRetryAttempts" value="3"/>
<property name="acquireRetryDelay" value="300" />
<property name="initialPoolSize" value=“3" />
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value=“3" />
</bean>

DBCL message on console

I am creating a database connection pool with following properties.
<bean id="complianceCCRDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="false" >
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
<property name="username" value="${deshaw.compliance.ccr.db.username}" />
<property name="password" value="${deshaw.compliance.ccr.db.password}" />
<property name="url" value="${deshaw.compliance.ccr.db.url}" />
<property name="initialSize" value="5" />
<property name="maxActive" value="10" />
<property name="maxWait" value="60000"/>
<property name="testOnBorrow" value="true" />
<property name="validationQuery">
<value>use ${deshaw.compliance.ccr.db.name} SELECT 1
</value>
</property>
<property name="maxIdle" value="10" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="600" />
<property name="logAbandoned" value="false" />
</bean>
When I try to instantiate the above bean, I am getting following messages on STDOUT. Is there any way I can suppress these ?
AbandonedObjectPool is used (org.apache.commons.dbcp.AbandonedObjectPool#2d82ef6b)
LogAbandoned: false
RemoveAbandoned: true
RemoveAbandonedTimeout: 600
You should use the setLogWriter() (see javadoc here) method as described in this JIRA.

Categories