OceanBase数据库连接-使用Druid数据库连接池连接
本文介绍了OceanBase数据库两种连接模式的配置方法。MySQL模式需配置连接串参数(host、port、database_name等),使用mysql-connector-java驱动;Oracle模式需配置schema_name等参数,使用oceanbase-client驱动。两种模式都需要在pom.xml中添加相应依赖,并在db.properties中配置数据库连接和连接池参数(如ini
一、基于MySQL模式
联系 OceanBase 数据库部署人员或者管理员获取相应的数据库连接串。
示例如下:
obclient -hxxx.xxx.xxx.xxx -P2881 -utest_user001@mysql001 -p****** -Dtest
参数说明:
-
$host
:提供 OceanBase 数据库连接 IP。OceanBase 数据库代理(OceanBase Database Proxy,ODP)连接方式使用的是一个 ODP 地址;直连方式使用的是 OBServer 节点的 IP 地址。 -
$port
:提供 OceanBase 数据库连接端口。ODP 连接的方式默认是2883
,在部署 ODP 时可自定义;直连方式默认是2881
,在部署 OceanBase 数据库时可自定义。 -
$database_name
:需要访问的数据库名称。 -
user_name
:提供租户的连接账户。ODP 连接的常用格式:用户名@租户名#集群名
或者集群名:租户名:用户名
;直连方式格式:用户名@租户名
。 -
password
:提供账户密码。
配置文件
-
pom.xml
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>druid-mysql-client</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> </dependencies> </project>
注意:如果连接失败,版本太低不符合也是会出现连接失败的情况!!!!
-
db.properties
-
# Database Configuration driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://$host:$port/$database_name?useSSL=false username=$user_name password=$password # Connection Pool Configuration #To check whether the database link is valid, MySQL must be configured to select 1; Oracle is select 1 from dual validationQuery=select 1 #Initial number of connections initialSize=3 #Maximum number of activations, that is, the maximum number of Connection pool maxActive=30 #When closing the Abandoned connection, the error log is output. When the link is recycled, the console prints information. The test environment can add true, while the online environment is false. Will affect performance. logAbandoned=true #Minimum number of activations during idle time minIdle=5 #The maximum waiting time for a connection, in milliseconds maxWait=1000 #The maximum time to start the eviction thread is the survival time of a link (previous value: 25200000, the converted result of this time is: 2520000/1000/60/60=7 hours) minEvictableIdleTimeMillis=300000 #Whether to recycle after exceeding the time limit removeAbandoned=true #Exceeding the time limit (in seconds), currently 5 minutes. If any business processing time exceeds 5 minutes, it can be adjusted appropriately. removeAbandonedTimeout=300 # Run the idle connection collector Destroy thread every 10 seconds to detect the interval time between connections, based on the judgment of testWhileIdle timeBetweenEvictionRunsMillis=10000 #When obtaining a link, not verifying its availability can affect performance. testOnBorrow=false #Check whether the link is available when returning the link to the Connection pool. testOnReturn=false #This configuration can be set to true, without affecting performance and ensuring security. The meaning is: Detect when applying for a connection. If the idle time is greater than timeBetweenEviceRunsMillis, execute validationQuery to check if the connection is valid. testWhileIdle=true #Default false, if configured as true, connection detection will be performed in the DestroyConnectionThread thread (timeBetweenEvaluation once) keepAlive=false #If keepAlive rule takes effect and the idle time of the connection exceeds it, the connection will only be detected keepAliveBetweenTimeMillis=60000
主要参考数据连接参数 # Database Configuration,其他根据实际情况调整
二、基于Oracle模式
联系 OceanBase 数据库部署人员或者管理员获取相应的数据库连接串。
示例如下:
obclient -hxxx.xxx.xxx.xxx -P2881 -utest_user001@oracel001 -p******
参数说明:
-
$host
:提供 OceanBase 数据库连接 IP。OceanBase 数据库代理(OceanBase Database Proxy,ODP)连接方式使用的是一个 ODP 地址;直连方式使用的是 OBServer 节点的 IP 地址。 -
$port
:提供 OceanBase 数据库连接端口。ODP 连接的方式默认是2883
,在部署 ODP 时可自定义;直连方式默认是2881
,在部署 OceanBase 数据库时可自定义。 -
$schema_name
:需要访问的 Schema 名称。 -
$user_name
:提供租户的连接账户。ODP 连接的常用格式:用户名@租户名#集群名
或者集群名:租户名:用户名
;直连方式格式:用户名@租户名
。 -
$password
:提供账户密码。
配置文件
-
pom.xml
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>druid-oceanbase-client</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.oceanbase</groupId> <artifactId>oceanbase-client</artifactId> <version>2.4.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> </dependencies> </project>
注意:如果连接失败,版本太低不符合也是会出现连接失败的情况!!!!
-
db.properties
-
# Database Configuration driverClassName=com.oceanbase.jdbc.Driver url=jdbc:oceanbase://$host:$port/$schema_name username=$user_name password=$password # Connection Pool Configuration #To check whether the database link is valid, MySQL must be configured to select 1; Oracle is select 1 from dual validationQuery=select 1 from dual #Initial number of connections initialSize=3 #Maximum number of activations, that is, the maximum number of Connection pool maxActive=30 #When closing the Abandoned connection, the error log is output. When the link is recycled, the console prints information. The test environment can add true, while the online environment is false. Will affect performance. logAbandoned=true #Minimum number of activations during idle time minIdle=5 #The maximum waiting time for a connection, in milliseconds maxWait=1000 #The maximum time to start the eviction thread is the survival time of a link (previous value: 25200000, the converted result of this time is: 2520000/1000/60/60=7 hours) minEvictableIdleTimeMillis=300000 #Whether to recycle after exceeding the time limit removeAbandoned=true #Exceeding the time limit (in seconds), currently 5 minutes. If any business processing time exceeds 5 minutes, it can be adjusted appropriately. removeAbandonedTimeout=300 # Run the idle connection collector Destroy thread every 10 seconds to detect the interval time between connections, based on the judgment of testWhileIdle timeBetweenEvictionRunsMillis=10000 #When obtaining a link, not verifying its availability can affect performance. testOnBorrow=false #Check whether the link is available when returning the link to the Connection pool. testOnReturn=false #This configuration can be set to true, without affecting performance and ensuring security. The meaning is: Detect when applying for a connection. If the idle time is greater than timeBetweenEviceRunsMillis, execute validationQuery to check if the connection is valid. testWhileIdle=true #Default false, if configured as true, connection detection will be performed in the DestroyConnectionThread thread (timeBetweenEvaluation once) keepAlive=false #If keepAlive rule takes effect and the idle time of the connection exceeds it, the connection will only be detected keepAliveBetweenTimeMillis=60000

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐
所有评论(0)