Robotframework + JayDeBeApi + SQLServer提供SQLServerException:当AutoCommit模式设置为" true"

时间:2015-12-02 17:10:12

标签: sql-server jdbc robotframework jaydebeapi

我使用robotframework DatabaseLibrary,JayDeBeApi,MS SQL Server JDBC驱动程序设置了机器人框架测试,并且连接成功。 但是我得到 SQLServerException:当运行任何sql时,当AutoCommit模式设置为" true" 时,无法调用回滚操作。

我尝试通过添加" autoCommit = false"来关闭AutoCommit。在jdbc连接url上或添加" SET IMPLICIT_TRANSACTIONS ON"在SQL语句中,甚至在SQL Server Management Studio上检查SET IMPLICIT_TRANSACTIONS ANSI选项,但是没有任何运气。

这是robotframework maven插件设置

的pom.xml

<plugin>
    <groupId>org.robotframework</groupId>
    <artifactId>robotframework-maven-plugin</artifactId>
    <version>1.4.5</version>
    <executions>
        <execution>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <extraPathDirectories>
            <extraPathDirectory>/opt/virtualenv_envs/robot/lib/python2.7/site-packages</extraPathDirectory>
            <extraPathDirectory>/usr/local/lib/python2.7/dist-packages</extraPathDirectory>
        </extraPathDirectories>
    </configuration>
    <dependencies>
        <dependency>  <!-- this jar from local nexus -->
            <groupId>com.microsoft.sqlserver.jdbc</groupId>
            <artifactId>sqljdbc41</artifactId>
            <version>4.1</version>
        </dependency>
    </dependencies>
</plugin>

以下是机器人测试

dbTest.robot

*** Settings ***
Library                         DatabaseLibrary
Library                         String
Library                         OperatingSystem
Suite Setup                     SuiteSetup
Suite Teardown                  SuiteTearDown 

*** Variables ***
${JDBC_DRIVER}                  'com.microsoft.sqlserver.jdbc.SQLServerDriver'
${JDBC_URL}                     'jdbc:sqlserver://{ipAddress}:1433;databaseName={myDB};user={username};password={password}'

*** Test Cases ***
Test Prepare Data
    [Documentation]             Reset Data
    Execute Sql String          UPDATE dbo.ImportHeader SET ExtractedOn=null;


*** Keywords ***
SuiteSetup
    Connect To Database Using Custom Params    jaydebeapi    ${JDBC_DRIVER},${JDBC_URL}

SuiteTearDown
    Disconnect From Database

这是我在运行机器人测试时遇到的错误

Test Prepare Data :: Reset Data...        | FAIL |
DatabaseError: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot invoke a rollback operation when the AutoCommit mode is set to "true".

我在这方面采取的一个重大解决方案是修改robotframework DatabaseLibary connection_manager.py,并在jaydebeapi连接上将autoCommit设置为false,如下面的 self._dbconnection.jconn.setAutoCommit(False),但我希望知道比如果有更好的解决方案。

{your python virtualenv} /site-packages/DatabaseLibrary/connection_manager.py

import ConfigParser
from robot.api import logger

class ConnectionManager(object):
    """
    Connection Manager handles the connection & disconnection to the database.
    """




    def connect_to_database_using_custom_params(self, dbapiModuleName=None, db_connect_string=''):
        """
        Loads the DB API 2.0 module given `dbapiModuleName` then uses it to 
        connect to the database using the map string `db_custom_param_string`.

        Example usage:
        | # for psycopg2 |
        | Connect To Database Using Custom Params | psycopg2 | database='my_db_test', user='postgres', password='s3cr3t', host='tiger.foobar.com', port=5432 |

        | # for JayDeBeApi | 
        | Connect To Database Using Custom Params | JayDeBeApi | 'oracle.jdbc.driver.OracleDriver', 'my_db_test', 'system', 's3cr3t' |
        """
        db_api_2 = __import__(dbapiModuleName)

        db_connect_string = 'db_api_2.connect(%s)' % db_connect_string

        self._dbconnection = eval(db_connect_string)
        self._dbconnection.jconn.setAutoCommit(False)

2 个答案:

答案 0 :(得分:0)

前段时间我也确实遇到过这个问题,但是我记得它在url中使用了autocommit(例子来自我的解决方案):

Connect To Database Using Custom Params    jaydebeapi     'net.sourceforge.jtds.jdbc.Driver', ['jdbc:jtds:sqlserver://${DB_HOST}:${DB_PORT}/${database};autocommit=false','${DB_USER}','${DB_PASSWORD}'],${jdbcDriver}

但不幸的是,它不能用于oracle,我必须像你一样修改库。

答案 1 :(得分:0)

您还可以在DatabaseLibrary实例中将autocommit设置为false:

Connect To Database Using Custom Params    jaydebeapi    'oracle.jdbc.driver.OracleDriver', 'jdbc:oracle:thin:@oracle.database.com:1234/foo', ['username_here', 'password_here'], '${CURDIR}/databasedrivers/ojdbc8.jar'
${DatabaseLibrary}    Get library instance    DatabaseLibrary
Evaluate    $DatabaseLibrary._dbconnection.jconn.setAutoCommit(False)