You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2018/12/13 13:39:00 UTC

[jira] [Commented] (AIRFLOW-766) Skip conn.commit() when in Auto-commit

    [ https://issues.apache.org/jira/browse/AIRFLOW-766?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16720179#comment-16720179 ] 

ASF GitHub Bot commented on AIRFLOW-766:
----------------------------------------

ashb closed pull request #2209: [AIRFLOW-766] Skip conn.commit() when in Auto-commit
URL: https://github.com/apache/incubator-airflow/pull/2209
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/hooks/dbapi_hook.py b/airflow/hooks/dbapi_hook.py
index df52e54ea9..753dea179f 100644
--- a/airflow/hooks/dbapi_hook.py
+++ b/airflow/hooks/dbapi_hook.py
@@ -170,7 +170,16 @@ def run(self, sql, autocommit=False, parameters=None):
             else:
                 cur.execute(s)
         cur.close()
-        conn.commit()
+
+        # Skip commit when autocommit is activated
+        if self.supports_autocommit and autocommit:
+            pass
+        elif not self.supports_autocommit and autocommit:
+            logging.warn(("%s connection doesn't support " +
+                "autocommit but autocommit activated: ")
+                % getattr(self, self.conn_name_attr))
+        else:
+            conn.commit()
         conn.close()
 
     def set_autocommit(self, conn, autocommit):
diff --git a/airflow/hooks/jdbc_hook.py b/airflow/hooks/jdbc_hook.py
index bc1f352ecc..5a1271f204 100644
--- a/airflow/hooks/jdbc_hook.py
+++ b/airflow/hooks/jdbc_hook.py
@@ -64,4 +64,4 @@ def set_autocommit(self, conn, autocommit):
         :param conn: The connection
         :return:
         """
-        conn.jconn.autocommit = autocommit
+        conn.jconn.setAutoCommit(autocommit)
diff --git a/airflow/utils/db.py b/airflow/utils/db.py
index 54254f61dd..a993dd8a76 100644
--- a/airflow/utils/db.py
+++ b/airflow/utils/db.py
@@ -253,6 +253,17 @@ def initdb():
         models.Connection(
             conn_id='databricks_default', conn_type='databricks',
             host='localhost'))
+    merge_conn(
+        models.Connection(
+            conn_id='jdbc_default', conn_type='jdbc',
+            host='jdbc:mysql://localhost:3306/airflow', port=3306,
+            login='airflow', password='airflow', schema='airflow',
+            extra='''
+            {
+                "extra__jdbc__drv_path": "/tmp/mysql-connector-java-5.1.40-bin.jar",
+                "extra__jdbc__drv_clsname": "com.mysql.jdbc.Driver"
+            }
+            '''))
 
     # Known event types
     KET = models.KnownEventType
diff --git a/tests/hooks/__init__.py b/tests/hooks/__init__.py
new file mode 100644
index 0000000000..a85b77269c
--- /dev/null
+++ b/tests/hooks/__init__.py
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
diff --git a/tests/hooks/dbapi_hook.py b/tests/hooks/dbapi_hook.py
new file mode 100644
index 0000000000..281ab033e7
--- /dev/null
+++ b/tests/hooks/dbapi_hook.py
@@ -0,0 +1,52 @@
+# -*- coding: utf-8 -*-
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+import logging
+import unittest
+import mock
+from mock import MagicMock, Mock
+from airflow.hooks.jdbc_hook import JdbcHook
+from airflow.hooks.postgres_hook import PostgresHook
+
+class TestDbApiHook(unittest.TestCase):
+
+    def test_set_autocommit(self):     
+        hook = JdbcHook(jdbc_conn_id='jdbc_default')
+        conn = MagicMock(name='conn')
+        conn.jconn.setAutoCommit = Mock(return_value=None)
+
+        hook.set_autocommit(conn, False)
+        conn.jconn.setAutoCommit.assert_called_with(False)
+
+        hook.set_autocommit(conn, True)
+        conn.jconn.setAutoCommit.assert_called_with(True)
+
+    def test_autocommit(self):
+        logging.info("Test autocommit when connection supports autocommit")
+        jdbc_hook = JdbcHook(jdbc_conn_id='jdbc_default')
+        jdbc_hook.run(sql='SELECT 1', autocommit=True)
+        self.assertTrue('Query ran success with supports_autocommit=True, autocommit=True')
+        jdbc_hook.run(sql='SELECT 1', autocommit=False)
+        self.assertTrue('Query ran success with supports_autocommit=True, autocommit=False')
+
+    def test_autocommit_unsupported(self):
+        logging.info("Test autocommit when connection doesn't support autocommit")
+        pg_hook = PostgresHook(conn_id='postgres_default')
+        pg_hook.run(sql='SELECT 1', autocommit=True)
+        self.assertTrue('Query ran success with supports_autocommit=True, autocommit=True')
+        pg_hook.run(sql='SELECT 1', autocommit=False)
+        self.assertTrue('Query ran success with supports_autocommit=True, autocommit=False')
+        
+if __name__ == '__main__':
+    unittest.main()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


> Skip conn.commit() when in Auto-commit
> --------------------------------------
>
>                 Key: AIRFLOW-766
>                 URL: https://issues.apache.org/jira/browse/AIRFLOW-766
>             Project: Apache Airflow
>          Issue Type: Bug
>          Components: db
>    Affects Versions: 2.0.0
>         Environment: Airflow 2.0, IBM Netezza
>            Reporter: Pfubar.k
>            Assignee: Pfubar.k
>            Priority: Major
>              Labels: easyfix
>             Fix For: 1.10.0
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> Some JDBC Drivers fails when using DbApiHook.run(), DbApiHook.insert_rows().
> I'm using IBM Netezza. When auto-commit mode is on I get this error message.
> {code}
> NzSQLException: The connection object is in auto-commit mode
> {code}
> conn.commit() needs to be called only when auto-commit mode is off.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)