gitbase jdbc 连接简单说明
以下是Hikari 连接gitbase 的一个简单说明,可以参考,同时附带解决问题的思路
- 参考代码
spring bean
@Bean
public DataSource dataSource(){
HikariConfig config = new HikariConfig();
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
config.setJdbcUrl("jdbc:mysql://<host>:3306/gitbase?useLocalSessionState=true&characterEncoding=UTF-8");
config.setUsername("<username>");
config.setPassword("<password>");
HikariDataSource hikariDataSource = new HikariDataSource(config);
return hikariDataSource;
}
- 问题说明
如果使用默认的参数jdbc:mysql://<host>:3306/gitbase?characterEncoding=UTF-8
出现的问题如下
Caused by: java.sql.SQLException: Could not retrieve transaction isolation level from server
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.mysql.cj.jdbc.ConnectionImpl.getTransactionIsolation(ConnectionImpl.java:1214) ~[mysql-connector-java-8.0.17.jar:8.0.17]
at com.zaxxer.hikari.pool.PoolBase.checkDefaultIsolation(PoolBase.java:479) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.checkDriverSupport(PoolBase.java:442) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:410) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:363) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar:na]
跟进源码发现hikari 走了prepareCall,gitbase是不支持此特性的,而且gitbase 是不支持事务的,我们应该关闭此选项
hikari PoolBase.java 调用处理
private void checkDriverSupport(final Connection connection) throws SQLException
{
if (!isValidChecked) {
checkValidationSupport(connection);
checkDefaultIsolation(connection);
isValidChecked = true;
}
}
加载mysql jdbc 源码之后发现了此方法
ConnectionImpl.java 文件
@Override
public int getTransactionIsolation() throws SQLException {
synchronized (getConnectionMutex()) {
// 此配置是核心
if (!this.useLocalSessionState.getValue()) {
String s = this.session.queryServerVariable(
versionMeetsMinimum(8, 0, 3) || (versionMeetsMinimum(5, 7, 20) && !versionMeetsMinimum(8, 0, 0)) ? "@@session.transaction_isolation"
: "@@session.tx_isolation");
if (s != null) {
Integer intTI = mapTransIsolationNameToValue.get(s);
if (intTI != null) {
this.isolationLevel = intTI.intValue();
return this.isolationLevel;
}
throw SQLError.createSQLException(Messages.getString("Connection.12", new Object[] { s }), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
getExceptionInterceptor());
}
throw SQLError.createSQLException(Messages.getString("Connection.13"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
return this.isolationLevel;
}
}
解决方法就很简单了,如果设置为true问题就可以解决了,因为默认为空
public ConnectionImpl(HostInfo hostInfo) throws SQLException {
try {
// Stash away for later, used to clone this connection for Statement.cancel and Statement.setQueryTimeout().
this.useLocalSessionState = this.propertySet.getBooleanProperty(PropertyKey.useLocalSessionState);
this.useServerPrepStmts = this.propertySet.getBooleanProperty(PropertyKey.useServerPrepStmts);
说明
以上只是一个思路,代码部分处理可能不同版本会不同,注意选择