druiddatasource 怎么配置多个连接

druiddatasource 怎么配置多个连接,第1张

配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来。

如果没有配置,将会生成一个名字,格式是:"DataSource-" + SystemidentityHashCode(this)

--解决办法:使用以下代码将远程查询超时值设置为 0(无限等待):

sp_configure 'remote query timeout', 0

go

reconfigure with override

go

Druild包获取

Maven工程中添加druid依赖包:

<!-- >

1在applicationproperties配置账号密码等信息

serverport=8080

serverservletcontext-path=/share-center-report

springthymeleafprefix=classpath:/templates/

springdatasourcetype=comalibabadruidpoolDruidDataSource

springdatasourcedruiddriver-class-name=oraclejdbcdriverOracleDriver

springdatasourceurl=

springdatasourceusername=

springdatasourcepassword=

2引入依赖

<dependency>

<groupId>comoracle</groupId>

<artifactId>ojdbc6</artifactId>

<version>11201</version>

</dependency>

<dependency>

<groupId>comalibaba</groupId>

<artifactId>druid</artifactId>

<version>1110</version>

</dependency>

3在启动类配置扫描mapper

@MapperScan({"comexampledemo1mapper"})

4编写demo测试

public interface testMapper {

@Select("select from test_data")

List<TestVo> test();

}

/ 自定义realm程序 /

public class MyRealm extends AuthorizingRealm{

@Resource

private testMapper testMapper;

@Override

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

Systemoutprintln("授权");

return null;

}

}

画外音:目前Druid在开源中国举办的2019年度最受欢迎中国开源软件中排名第7名,支持Druid的朋友可以去投票哇。 2019年度最受欢迎中国开源软件

testOnReturn :如果为true(默认false),当应用使用完连接,连接池回收连接的时候会判断该连接是否还可用。

当连接使用完,调用commit或者rollback方法后,连接池会回收该连接,该参数主要在DruidDataSource的recycle方法中用到

判断连接是否可用同testOnBorrow

Druid配置参数详解-testOnBorrow

<obj name="dataSource" type="comalibabadruidpoolDruidDataSource">

<field name="driverClassName"><str>oraclejdbcdriverOracleDriver</str></field>

<field name="url"><str>jdbc:oracle:thin:@127001:1521:tsoc</str></field>

<field name="username"><str>user</str></field>

<field name="password"><str>123456</str></field>

<field name="maxActive"><int>500</int></field>

<field name="minIdle"><int>30</int></field>

<!-- 配置获取连接等待超间 -->

<field name="maxWait"><int>10000</int></field>

<!-- 配置间隔久才进行检测检测需要关闭空闲连接单位毫秒 -->

<field name="timeBetweenEvictionRunsMillis"><int>60000</int></field>

<!-- 配置连接池存间单位毫秒 -->

<field name="minEvictableIdleTimeMillis"><int>300000</int></field>

<field name="testWhileIdle"><bool>true</bool></field>

<field name="testOnBorrow"><bool>false</bool></field>

<field name="testOnReturn"><bool>false</bool></field>

<field name="defaultAutoCommit"><bool>true</bool></field>

<field name="validationQuery"><str>select 1 + 1</str></field>

</obj>

补充:数据库连接并没耗尽使用select from v$session才36使用其测试程序或者客户端工具都连接数据库

转载仅供参考!

druidproperties

url=jdbc:mysql://localhost:3306/mms

username=root

password=root

driverClassName=commysqljdbcDriver

JDBCUtilsjava

public class JDBCUtils {

/

使用Druid数据库连接池技术

/

private static DataSource source;

static {

try {

Properties pros = new Properties();

InputStream is = ClassLoadergetSystemClassLoader()getResourceAsStream("druidproperties");

prosload(is);

source = DruidDataSourceFactorycreateDataSource(pros);

} catch (Exception e) {

eprintStackTrace();

}

}

public static Connection getConnection() throws SQLException {

Connection conn = sourcegetConnection();

return conn;

}

/

使用dbutilsjar中提供的DbUtils工具类,实现资源的关闭

/

public static void closeResource(Connection conn, Statement ps, ResultSet rs) {

DbUtilscloseQuietly(conn);

DbUtilscloseQuietly(ps);

DbUtilscloseQuietly(rs);

}

}

Testjava

public class Test {

static Connection conn = null;

public static void main(String[] args) throws SQLException {

conn = JDBCUtilsgetConnection();

Systemoutprintln(conn);

}

}

jar包:

以上就是关于druiddatasource 怎么配置多个连接全部的内容,包括:druiddatasource 怎么配置多个连接、连接池druid连接SqlServer 2008问题、如何使用Druid监控sql等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9740471.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存