Skip to content

Commit

Permalink
BugFix: Fix unable to connect to oracle tenant.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanoOo committed Nov 28, 2024
1 parent 8894ff3 commit 12bebc4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
8 changes: 7 additions & 1 deletion docs/spark-connector-oceanbase.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ df.write
</tr>
<tr>
<td>sql-port</td>
<td style="word-wrap: break-word;"></td>
<td style="word-wrap: break-word;">2881</td>
<td>Integer</td>
<td>The SQL port.</td>
</tr>
Expand Down Expand Up @@ -313,6 +313,12 @@ df.write
<td>String</td>
<td>The table name.</td>
</tr>
<tr>
<td>driver</td>
<td style="word-wrap: break-word;">com.mysql.cj.jdbc.Driver</td>
<td>String</td>
<td>The class name of the JDBC driver. By default, it connects to the MySQL tenant. If you need to connect to Oracle tenant, the name needs to be <code>com.oceanbase.jdbc.Driver</code></td>
</tr>
</tbody>
</table>
</div>
Expand Down
6 changes: 6 additions & 0 deletions docs/spark-connector-oceanbase_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ df.write
<td>String</td>
<td>表名。</td>
</tr>
<tr>
<td>driver</td>
<td style="word-wrap: break-word;">com.mysql.cj.jdbc.Driver</td>
<td>String</td>
<td>JDBC 驱动程序的类名。默认支持连接MySQL租户。如果需要连接到Oracle租户,请修改为 <code>com.oceanbase.jdbc.Driver</code></td>
</tr>
</tbody>
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface ConnectionOptions {
String PASSWORD = "password";
String SCHEMA_NAME = "schema-name";
String TABLE_NAME = "table-name";
String DRIVER = "driver";
String DRIVER_DEFAULT = "com.mysql.cj.jdbc.Driver";

/* Direct-load config */
String ENABLE_DIRECT_LOAD_WRITE = "direct-load.enabled";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import java.sql.{Connection, DriverManager}
object OBJdbcUtils {
val OB_MYSQL_URL = s"jdbc:mysql://%s:%d/%s"
private val OB_ORACLE_URL = s"jdbc:oceanbase://%s:%d/%s"
private val MYSQL_JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"
private val MYSQL_LEGACY_JDBC_DRIVER = "com.mysql.jdbc.Driver"
private val OB_JDBC_DRIVER = "com.oceanbase.jdbc.Driver"
private val OB_LEGACY_JDBC_DRIVER = "com.alipay.oceanbase.jdbc.Driver"

def getConnection(sparkSettings: SparkSettings): Connection = {
val connection = DriverManager.getConnection(
Expand All @@ -41,19 +45,28 @@ object OBJdbcUtils {

def getJdbcUrl(sparkSettings: SparkSettings): String = {
var url: String = null
if ("MYSQL".equalsIgnoreCase(getCompatibleMode(sparkSettings))) {
val driver =
sparkSettings.getProperty(ConnectionOptions.DRIVER, ConnectionOptions.DRIVER_DEFAULT)
if (
driver.equalsIgnoreCase(MYSQL_JDBC_DRIVER) || driver.equalsIgnoreCase(
MYSQL_LEGACY_JDBC_DRIVER)
) {
url = OBJdbcUtils.OB_MYSQL_URL.format(
sparkSettings.getProperty(ConnectionOptions.HOST),
sparkSettings.getIntegerProperty(ConnectionOptions.SQL_PORT),
sparkSettings.getProperty(ConnectionOptions.SCHEMA_NAME)
)
} else {
} else if (
driver.equalsIgnoreCase(OB_JDBC_DRIVER) || driver.equalsIgnoreCase(OB_LEGACY_JDBC_DRIVER)
) {
JdbcDialects.registerDialect(OceanBaseOracleDialect)
url = OBJdbcUtils.OB_ORACLE_URL.format(
sparkSettings.getProperty(ConnectionOptions.HOST),
sparkSettings.getIntegerProperty(ConnectionOptions.SQL_PORT),
sparkSettings.getProperty(ConnectionOptions.SCHEMA_NAME)
)
} else {
throw new RuntimeException(String.format("Unsupported driver name: %s", driver))
}
url
}
Expand Down

0 comments on commit 12bebc4

Please sign in to comment.