http://theblasfrompas.blogspot.com.au/2013/12/hikaricp-connection-pool-with-pivotal.html
In this example I show how you would use this Connection Pool with Spring.
1. Ensure your using version 1.2.1 of HikariCP as it provides the setDataSourceProperties() setter to HikariConfig to allow easier configuration though Spring. Example below is for Maven.
<dependencies> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>1.2.1</version> <scope>compile</scope> </dependency>
2. Create a spring configuration XML file as shown below.
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"> <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"> <property name="maximumPoolSize" value="10" /> <property name="minimumPoolSize" value="2" /> <property name="dataSourceClassName" value="com.vmware.sqlfire.internal.jdbc.ClientDataSource" /> <property name="dataSourceProperties" ref="props" /> <property name="poolName" value="springHikariCP" /> </bean> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"> <constructor-arg ref="hikariConfig" /> </bean> <util:properties id="props" location="classpath:datasource.properties"/> </beans>
3. Create a datasource.properties as shown below.
portNumber=1527
serverName=192.168.1.6
user=app
password=app
4. Finally access with code as follows
ApplicationContextHolder.java
package pivotal.au.gemfirexd.demos.connectionpool.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextHolder { private static ClassPathXmlApplicationContext applicationContext; public static ClassPathXmlApplicationContext getInstance() { if(applicationContext == null) { applicationContext = new ClassPathXmlApplicationContext("classpath:application-context.xml"); } return applicationContext; } public static ApplicationContext getInstance(String contextLocation) { if(applicationContext == null) { applicationContext = new ClassPathXmlApplicationContext(contextLocation); } return applicationContext; } }
Accessing as follows
package pivotal.au.gemfirexd.demos.connectionpool.spring; import com.zaxxer.hikari.HikariDataSource; import org.springframework.context.ApplicationContext; import java.sql.Connection; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; import java.sql.*; public class TestHikariCPSpring { private ApplicationContext context; private Logger logger = Logger.getLogger(this.getClass().getSimpleName()); public void run() throws SQLException { context = getContext(); HikariDataSource ds = (HikariDataSource) context.getBean("dataSource"); Connection conn = ds.getConnection(); .....
No comments:
Post a Comment