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'