1、配置druid连接池
pom引入
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.11</version> </dependency>
spring-context配置
<!-- druid数据库连接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 数据库基本信息配置 --> <property name="url" value="${druid_url}" /> <property name="username" value="${druid_username}" /> <property name="password" value="${druid_password}" /> <property name="driverClassName" value="${druid_driver}" /> <property name="filters" value="${filters}" /> <!-- 最大并发连接数 --> <property name="maxActive" value="${maxActive}" /> <!-- 初始化连接数量 --> <property name="initialSize" value="${initialSize}" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${maxWait}" /> <!-- 最小空闲连接数 --> <property name="minIdle" value="${minIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> <property name="testOnReturn" value="${testOnReturn}" /> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}" /> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="${removeAbandoned}" /> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" /> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${logAbandoned}" /> </bean> <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter" > <property name="statementExecutableSqlLogEnable" value="true" /> </bean>
web.xml配置
<!-- 可以使用Web应用、URI监控、Session监控等功能 --> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value> </init-param> <init-param> <param-name>profileEnable</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>resetEnable</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>principalCookieName</param-name> <param-value>USER_COOKIE</param-value> </init-param> <init-param> <param-name>principalSessionName</param-name> <param-value>USER_SESSION</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 查看内置监控页面,路径是/druid/index.html --> <servlet> <description>配置好用于查看内置监控页面,路径是/druid/index.html</description> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <!-- 可选项 登陆的用户名和密码 --> <init-param> <param-name>loginUsername</param-name> <param-value>druid</param-value> </init-param> <init-param> <param-name>loginPassword</param-name> <param-value>druid</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
2、配置HikariCP连接池
pom引入
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>4.0.3</version> </dependency>
spring-context配置
<!-- 配置数据源 --> <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> <property name="driverClassName" value="${db_driver}" /> <property name="jdbcUrl" value="${db_url}" /> <property name="username" value="${db_username}" /> <property name="password" value="${db_password}" /> <!-- HikariCP 配置 --> <property name="maximumPoolSize" value="10" /> <property name="minimumIdle" value="5" /> <property name="idleTimeout" value="300000" /> <property name="maxLifetime" value="1800000" /> <property name="connectionTimeout" value="30000" /> <property name="validationTimeout" value="5000" /> <property name="leakDetectionThreshold" value="0" /> </bean>
标签:数据源,配置,druid,alibaba,spring,com From: https://www.cnblogs.com/jinzhiming/p/18397971