From 7f0d1d8d3e23564d81df55fe55ecc62ef1f76d50 Mon Sep 17 00:00:00 2001 From: Altair Bow Date: Mon, 16 Sep 2024 16:35:34 +0800 Subject: [PATCH] jdbc: add JdbcDialectMixin --- dj_db_conn_pool/backends/jdbc/mixins.py | 9 +++++++++ dj_db_conn_pool/backends/jdbc/oceanbase/mysql/base.py | 10 +++------- dj_db_conn_pool/backends/jdbc/oceanbase/oracle/base.py | 10 +++------- dj_db_conn_pool/backends/jdbc/oracle/base.py | 10 +++------- 4 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 dj_db_conn_pool/backends/jdbc/mixins.py diff --git a/dj_db_conn_pool/backends/jdbc/mixins.py b/dj_db_conn_pool/backends/jdbc/mixins.py new file mode 100644 index 0000000..316534c --- /dev/null +++ b/dj_db_conn_pool/backends/jdbc/mixins.py @@ -0,0 +1,9 @@ +import jpype.dbapi2 + + +class JdbcDialectMixin: + def do_ping(self, dbapi_connection): + try: + return super().do_ping(dbapi_connection) + except jpype.dbapi2.DatabaseError: + return False diff --git a/dj_db_conn_pool/backends/jdbc/oceanbase/mysql/base.py b/dj_db_conn_pool/backends/jdbc/oceanbase/mysql/base.py index ce81c44..bb8c838 100644 --- a/dj_db_conn_pool/backends/jdbc/oceanbase/mysql/base.py +++ b/dj_db_conn_pool/backends/jdbc/oceanbase/mysql/base.py @@ -1,14 +1,10 @@ -import jpype.dbapi2 from django.db.backends.mysql import base from sqlalchemy.dialects.mysql.base import MySQLDialect +from dj_db_conn_pool.backends.jdbc.mixins import JdbcDialectMixin from dj_db_conn_pool.backends.jdbc.oceanbase.mixins import JDBCOceanBaseDatabaseWrapperMixin class DatabaseWrapper(JDBCOceanBaseDatabaseWrapperMixin, base.DatabaseWrapper): - class SQLAlchemyDialect(MySQLDialect): - def do_ping(self, dbapi_connection): - try: - return super().do_ping(dbapi_connection) - except jpype.dbapi2.DatabaseError: - return False + class SQLAlchemyDialect(JdbcDialectMixin, MySQLDialect): + pass diff --git a/dj_db_conn_pool/backends/jdbc/oceanbase/oracle/base.py b/dj_db_conn_pool/backends/jdbc/oceanbase/oracle/base.py index 0863075..9b19aa1 100644 --- a/dj_db_conn_pool/backends/jdbc/oceanbase/oracle/base.py +++ b/dj_db_conn_pool/backends/jdbc/oceanbase/oracle/base.py @@ -1,17 +1,13 @@ -import jpype.dbapi2 from django.db.backends.oracle import base from sqlalchemy.dialects.oracle.base import OracleDialect +from dj_db_conn_pool.backends.jdbc.mixins import JdbcDialectMixin from dj_db_conn_pool.backends.jdbc.oceanbase.mixins import JDBCOceanBaseDatabaseWrapperMixin class DatabaseWrapper(JDBCOceanBaseDatabaseWrapperMixin, base.DatabaseWrapper): - class SQLAlchemyDialect(OracleDialect): - def do_ping(self, dbapi_connection): - try: - return super().do_ping(dbapi_connection) - except jpype.dbapi2.DatabaseError: - return False + class SQLAlchemyDialect(JdbcDialectMixin, OracleDialect): + pass def init_connection_state(self): # TODO: custom OceanBase (Oracle mode) connection initialization diff --git a/dj_db_conn_pool/backends/jdbc/oracle/base.py b/dj_db_conn_pool/backends/jdbc/oracle/base.py index 44edda7..87d746a 100644 --- a/dj_db_conn_pool/backends/jdbc/oracle/base.py +++ b/dj_db_conn_pool/backends/jdbc/oracle/base.py @@ -4,11 +4,11 @@ import socket from multiprocessing import current_process -import jpype.dbapi2 from django.db.backends.oracle import base from sqlalchemy.dialects.oracle.base import OracleDialect from dj_db_conn_pool.backends.jdbc import JDBCDatabaseWrapperMixin +from dj_db_conn_pool.backends.jdbc.mixins import JdbcDialectMixin logger = logging.getLogger(__name__) @@ -21,12 +21,8 @@ class DatabaseWrapper(JDBCDatabaseWrapperMixin, base.DatabaseWrapper): - class SQLAlchemyDialect(OracleDialect): - def do_ping(self, dbapi_connection): - try: - return super().do_ping(dbapi_connection) - except jpype.dbapi2.DatabaseError: - return False + class SQLAlchemyDialect(JdbcDialectMixin, OracleDialect): + pass jdbc_driver = 'oracle.jdbc.OracleDriver'