C3p0整合Impala
- C3p0依赖
<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
-
Impala关键依赖需要到www.cloudera.com下载jar自己编译到mvn库区去,不知道
image.pngcloudera
为啥不把jar发布到mvn中央库去,扎心。。
-
将连接池托管到Spring容器去
package com.cmrh.touchlog.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/**
* @author GuoGuiRong
* @Description: C3P0核心配置
* @date 2018/9/12 9:40
*/
@Configuration
public class ConnPoolConfig {
@Value("${c3p0.DriverClass}")
private String driverClass;
@Value("${c3p0.initialPoolSize}")
private Integer initialPoolSize;
@Value("${c3p0.maxIdleTime}")
private Integer maxIdleTime;
@Value("${c3p0.JdbcUrl}")
private String jdbcUrl;
@Value("${c3p0.AcquireIncrement}")
private Integer acquireIncrement;
@Value("${c3p0.MinPoolSize}")
private Integer minPoolSize;
@Value("${c3p0.MaxPoolSize}")
private Integer maxPoolSize;
@Bean(name = "baseDataSource")
@Primary
public DataSource dataSource(){
//@Primary 注解作用是当程序选择dataSource时选择被注解的这个
ComboPooledDataSource cpds = new ComboPooledDataSource();
try {
cpds.setDriverClass(driverClass);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
cpds.setJdbcUrl(jdbcUrl);
cpds.setInitialPoolSize(initialPoolSize);
cpds.setMaxIdleTime(maxIdleTime);
cpds.setMinPoolSize(minPoolSize);
cpds.setAcquireIncrement(acquireIncrement);
cpds.setMaxPoolSize(minPoolSize);
return cpds;
}
}
- 封装工具类
package com.cmrh.touchlog.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.*;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
/**
* @author GuoGuiRong
* @Description:
* @date 2018/9/12 9:54
*/
@Component
@Slf4j
public class ImpalaUtil {
@Autowired
DataSource baseDataSource;
/**
* 执行语句有返回值
* @param sql
* @param handler
* @param <T>
* @return
*/
public <T> Optional<T> executeQuery(String sql,Function<? super ResultSet,T> handler){
ResultSet resultSet = null;
Connection connection = null;
Statement statement = null;
try {
connection = baseDataSource.getConnection();
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
return Optional.of(handler.apply(resultSet));
} catch (SQLException e) {
releaseResource(connection,statement,resultSet);
e.printStackTrace();
log.error("执行语句时出错了e={}",e);
}finally {
releaseResource(connection,statement,resultSet);
}
return Optional.empty();
}
/**
* 关闭连接,释放资源
* @param conn
* @param statement
* @param rs
*/
public void releaseResource(Connection conn, Statement statement, ResultSet rs) {
try {
if (rs != null) {
try {
rs.close();
} catch (Throwable e) {
e.printStackTrace();
log.error("出错了e={},关闭rs失败",e);
}
}
if (statement != null) {
try {
statement.close();
} catch (Throwable e) {
e.printStackTrace();
log.error("出错了e={},关闭statement失败",e);
}
}
if (conn != null) {
try {
conn.close();
} catch (Throwable e) {
e.printStackTrace();
log.error("出错了e={},关闭conn失败",e);
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
- C3P0参数说明:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><!--acquireIncrement:链接用完了自动增量3个。 -->
<property name="acquireIncrement">3</property>
<!--acquireRetryAttempts:链接失败后重新试30次。-->
<property name="acquireRetryAttempts">30</property>
<!--acquireRetryDelay;两次连接中间隔1000毫秒。 -->
<property name="acquireRetryDelay">1000</property>
<!--autoCommitOnClose:连接关闭时默认将所有未提交的操作回滚。 -->
<property name="autoCommitOnClose">false</property>
<!--automaticTestTable:c3p0测试表,没什么用。-->
<property name="automaticTestTable">Test</property>
<!--breakAfterAcquireFailure:出错时不把正在提交的数据抛弃。-->
<property name="breakAfterAcquireFailure">false</property>
<!--checkoutTimeout:100毫秒后如果sql数据没有执行完将会报错,如果设置成0,那么将会无限的等待。 -->
<property name="checkoutTimeout">100</property>
<!--connectionTesterClassName:通过实现ConnectionTester或QueryConnectionTester的类来测试连接。类名需制定全路径。Default: com.mchange.v2.c3p0.impl.DefaultConnectionTester-->
<property name="connectionTesterClassName"></property>
<!--factoryClassLocation:指定c3p0 libraries的路径,如果(通常都是这样)在本地即可获得那么无需设置,默认null即可。-->
<property name="factoryClassLocation">null</property>
<!--forceIgnoreUnresolvedTransactions:作者强烈建议不使用的一个属性。-->
<property name="forceIgnoreUnresolvedTransactions">false</property>
<!--idleConnectionTestPeriod:每60秒检查所有连接池中的空闲连接。-->
<property name="idleConnectionTestPeriod">60</property>
<!--initialPoolSize:初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。 -->
<property name="initialPoolSize">3</property>
<!--maxIdleTime:最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。-->
<property name="maxIdleTime">60</property>
<!--maxPoolSize:连接池中保留的最大连接数。 -->
<property name="maxPoolSize">15</property>
<!--maxStatements:最大链接数。-->
<property name="maxStatements">100</property>
<!--maxStatementsPerConnection:定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->
<property name="maxStatementsPerConnection"></property>
<!--numHelperThreads:异步操作,提升性能通过多线程实现多个操作同时被执行。Default: 3-->
<property name="numHelperThreads">3</property>
<!--overrideDefaultUser:当用户调用getConnection()时使root用户成为去获取连接的用户。主要用于连接池连接非c3p0的数据源时。Default: null-->
<property name="overrideDefaultUser">root</property>
<!--overrideDefaultPassword:与overrideDefaultUser参数对应使用的一个参数。Default: null-->
<property name="overrideDefaultPassword">password</property>
<!--password:密码。Default: null-->
<property name="password"></property>
<!--preferredTestQuery:定义所有连接测试都执行的测试语句。在使用连接测试的情况下这个一显著提高测试速度。注意: 测试的表必须在初始数据源的时候就存在。Default: null-->
<property name="preferredTestQuery">select id from test where id=1</property>
<!--propertyCycle:用户修改系统配置参数执行前最多等待300秒。Default: 300 -->
<property name="propertyCycle">300</property>
<!--testConnectionOnCheckout:因性能消耗大请只在需要的时候使用它。Default: false -->
<property name="testConnectionOnCheckout">false</property>
<!--testConnectionOnCheckin:如果设为true那么在取得连接的同时将校验连接的有效性。Default: false -->
<property name="testConnectionOnCheckin">true</property>
<!--user:用户名。Default: null-->
<property name="user">root</property>
<!--usesTraditionalReflectiveProxies:动态反射代理。Default: false-->
<property name="usesTraditionalReflectiveProxies">false</property></pre>
[![`](https://img.haomeiwen.com/i4051767/0f0f9b1938512e88.gif?imageMogr2/auto-orient/strip)](javascript:void(0); "复制代码")
网友评论