how to make below jdbc connection configuration connection pooling - java

i am having a web application and my application is connected to database for every hit to my web application i need to make a connection to database so i want to reduce time by caching or pooling connection
below is my database configured file
xml file:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>

Please use HikariCP for your application, best and very fast connection pool available in java world,configuration should be like below.
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" >
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/" />
<property name="maximumPoolSize" value="20" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="poolName" value="my-pool1" />
</bean>
</constructor-arg>
</bean>
Or you can have programatic configuration as well.
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://localhost:3306/simpsons");
config.setUsername("bart");
config.setPassword("51mp50n");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(config);

You can use as follows , just change your database driver and url settings
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
<property name="url" value="jdbc:oracle:thin:#localhost:1521:Employee" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
<property name="removeAbandoned" value="true"/>
<property name="initialSize" value="20" />
<property name="maxActive" value="30" />
</bean>

You should use SingleConnectionDataSource as datasource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</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;*/

Multiple DataSource Switching problem in Spring MVC. when the 2nd dataSource is unavailable it automatically uses 1st datasource where 2nd is referred

I have configured a 2 dataSources in my SpringMVC Project, but when the 2nd
dataSource is unavailable it automatically uses 1st datasource where 2nd is referred.
I want to stop this switching.
Here is the code:
dispatcher-servlet.xml:
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="defaultAutoCommit" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="1"/>
<property name="maxWait" value="500"/>
<property name="maxIdle" value="2"/>
</bean>
<bean id="dataSource1" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="false">
<property name="driverClassName" value="${jdbc.driverClassName1}"/>
<property name="url" value="${jdbc.url1}"/>
<property name="username" value="${jdbc.username1}"/>
<property name="password" value="${jdbc.password1}"/>
<property name="defaultAutoCommit" value="true"/>
<property name="testOnBorrow" value="true"/>
<property name="initialSize" value="1"/>
<property name="maxActive" value="3"/>
<property name="maxWait" value="500"/>
<property name="maxIdle" value="8"/>
</bean>
BaseNamedParameterJdbcDaoSupport.java class:
public class BaseNamedParameterJdbcDaoSupport extends NamedParameterJdbcDaoSupport{
#Autowired
public void setDataSourceFor1(DataSource dataSource) {
// System.out.println("Main DS"+dataSource);
setDataSource(dataSource);
}
}
BaseNamedParameterJdbcDaoSupportForMirrorDB.java :
public class BaseNamedParameterJdbcDaoSupportForMirrorDB extends NamedParameterJdbcDaoSupport{
#Autowired
public void setDataSourceFor2(DataSource dataSource1) {
// System.out.println("ForMirrorDB dataSource1"+dataSource1);
setDataSource(dataSource1);
}
}
It is not possible that the DataSource which are maintained with different name will automatically switch. I think you have implemented some other logic to work with these two DataSource objects.
Check your DAO/Service code to utilizing the "BaseNamedParameterJdbcDaoSupport" & "BaseNamedParameterJdbcDaoSupportForMirrorDB" with proper #Autowired.

Check Datasource connection during initialization

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>

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>

Configure spring datasource for hibernate and #Transactional

At this moment I'm using DriverManagerDataSource with #Transactional annotation to manage transactions. But all transactions are very very slow, probably because data source open and close connection to db each time.
What data source should I use to speed up transaction?
I am using in my application combination of two approaches. the first one is c3p0 connection pooling, its almost the same solution as chkal sugested. The second approach is to use Spring lazyConnectionDataSourceProxy, which creates lazy loading proxy that loads connection only if you hit the database. This is very useful, when you have second level cache and you are only reading cached data and queries - database wont be hit, and you don't need to acquire connection (which is pretty expensive).
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Pool properties -->
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="120" />
<property name="maxIdleTime" value="1200" />
</bean>
<bean name="lazyConnectionDataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="dataSource" />
</bean>
DriverManagerDataSource isn't actually a connection pool and should only be used for testing. You should try BasicDataSource from Apache Commons DBCP. Something like:
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

Categories