OceanBase 数据库支持JDBC驱动连接。经常有人问怎么连接,这里就简单说明一下。
驱动用法
OceanBase实例(租户)根据兼容性分 MySQL 和 ORACLE 两种。如果是连接 OB 的 MySQL 租户,可以使用 MySQL JDBC 驱动,版本推荐 5.1.46 。这个版本在内部大量使用验证过。当然也不是说其他版本就不行。此外也可以使用 OB 的 JDBC 驱动。组件名称后期可能会变化,实际文件名是 : oceanbase-client-1.1.10.jar
。版本号后面也会变,记住名字格式就行。如果是连接 OB 的 ORACLE 租户,则只能使用 OB 的 JDBC 驱动,不能使用 ORACLE 的 JDBC 驱动。
OB 的 JDBC 驱动加载类名推荐:com.alipay.oceanbase.jdbc.Driver
。以前的版本也有使用 com.alipay.oceanbase.obproxy.mysql.jdbc.Driver
的。对于大部分连接中间件,配置到 OB 的连接,只需要修改配置文件即可。各个配置文件可能会多少不一样,下面是示例供参考,关键是连接的 IP
、PORT
和 USER
。驱动的其他属性推荐开启 rewriteBatchedStatements
和 allowMultiQueries
。但是 useServerPrepStmts
在 OB 2.2 不建议开启。OB 2.2 之前应该不支持, 2.2 之后需要开启OB 集群参数 _ob_enable_prepared_statement
。2.2.7 以后的版本这个参数默认为 true
。
driver=com.alipay.oceanbase.obproxy.mysql.jdbc.Driver
conn=jdbc:oceanbase://127.1:2883/tpcc?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true&allowMultiQueries=true
user=tpcc@oboracle#obdemo
password=123456
下面是简单的JAVA 代码示例。
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.alipay.oceanbase.jdbc.Driver");
dataSource.setUrl("jdbc:oceanbase://" + ip + ":" + port + "/" + dbName + "?rewriteBatchedStatements=true&allowMultiQueries=true&useLocalSessionState=true&useUnicode=true&characterEncoding=utf-8&socketTimeout=3000000&userAffectedRows=true");
dataSource.setUsername(user);
dataSource.setPassword(passwd);
dataSource.setMaxIdle(6000);
dataSource.setMaxActive(6000);
dataSource.setPoolPreparedStatements(true);
dataSource.setMaxOpenPreparedStatements(60000);
其他的数据库操作写法跟传统数据库的 JDBC 驱动一样。
PreparedStatement
用法
//DBStatement dbst = new DBStatement(connection);
try {
PreparedStatement pstmt = connection.prepareStatement(sql);
try {
ResultSet rs = pstmt.executeQuery();
try {
List<MemRow> rows = getRows(rs, rs.getMetaData());
if (toCache) {
setCache(rows);
} else {
setRows(rows);
}
} finally {
rs.close();
}
} catch (SQLNonTransientConnectionException | SQLRecoverableException e) {
throw e;
} catch (Exception e) {
Error.raise(RBPM_CORE_0010, viewName, sql, e.getMessage());
} finally {
pstmt.close();
}
} finally {
CallableStatement
用法
public class MainCall {
public static void main(String[] args) {
try {
Driver driver = new com.alipay.oceanbase.jdbc.Driver();
Properties p = new Properties();
p.setProperty("", "");
Connection conn = DriverManager.getConnection("jdbc:oceanbase://127.1:2883/bankcore",
"bankcore@oboracle#obdemo", "123456");
// Create PROCEDURE 'calc_add'
Statement stmt = conn.createStatement();
String sql = "create or replace PROCEDURE calc_add(a1 IN int, a2 IN OUT int) is "
+ "begin " + " a2:=a1+a2; " + "end;";
stmt.execute(sql);
//Execute PROCEDURE 'calc_add'
CallableStatement csmt = conn.prepareCall("call calc_add(?, ?)");
csmt.setInt(1, 1);
csmt.setInt(2, 2);
csmt.registerOutParameter(2, Types.INTEGER);
csmt.execute();
//Get Result of a2 from 'calc_add'
int sum = csmt.getInt(1);
System.out.println("sum:" + sum);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
其他建议
- 使用连接池技术,连接池注意设置
keepAlive 属性
,即避免空闲连接。连接空闲太久可能会被 OB 或者网络上其他设备中断。 - 数据库操作要有异常处理逻辑。如果是连接中断,则新建连接;如果是事务异常,则回滚事务。事务长期不提交很容易触发 OB 的事务未提交超时或者空闲超时机制。如果是事务未提交超时,该连接必须明确发起
commit
或 rollback
以清理连接的事务状态。否则这个连接不能再使用(会报事务超时错误)。