简体   繁体   中英

how to make below jdbc connection configuration connection pooling

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM