GithubHelp home page GithubHelp logo

Comments (2)

waeilua avatar waeilua commented on May 27, 2024

i think this may helpful


template <typename F>
struct finally_action
{
    finally_action(F f) : clean(f) {}
    ~finally_action() { clean(); }
    F clean;
};

template <class F>
finally_action<F> finally(F f)
{
    return finally_action<F>(f);
}

namespace OpenLogReplicator {
    DatabaseConnection::DatabaseConnection(DatabaseEnvironment* env, string& user, string& password, string& server, bool sysASM) :
        env(env),
        errhp(nullptr),
        srvhp(nullptr),
        svchp(nullptr),
        authp(nullptr) {

        OCIHandleAlloc((dvoid*) env->envhp, (dvoid**) &errhp, OCI_HTYPE_ERROR, 0, nullptr);
        OCIHandleAlloc((dvoid*) env->envhp, (dvoid**) &srvhp, OCI_HTYPE_SERVER, 0, nullptr);
        OCIHandleAlloc((dvoid*) env->envhp, (dvoid**) &svchp, OCI_HTYPE_SVCCTX, 0, nullptr);
        OCIHandleAlloc((dvoid*) env->envhp, (dvoid**) &authp, OCI_HTYPE_SESSION, 0, nullptr);

        bool clean = true;
        auto failClean = finally([&clean,this]()
        {
                if (clean)
                {
                    //same as ~DatabaseConnection()
                    OCISessionEnd(svchp, errhp, nullptr, OCI_DEFAULT);
                    OCIServerDetach(srvhp, errhp, OCI_DEFAULT);

                    if (authp != nullptr) {
                        OCIHandleFree((dvoid*)authp, OCI_HTYPE_SESSION);
                        authp = nullptr;
                    }

                    if (errhp != nullptr) {
                        OCIServerDetach(srvhp, errhp, OCI_DEFAULT);
                        errhp = nullptr;
                    }

                    if (svchp != nullptr) {
                        OCIHandleFree((dvoid*)svchp, OCI_HTYPE_SVCCTX);
                        svchp = nullptr;
                    }

                    if (authp != nullptr) {
                        OCIHandleFree((dvoid*)authp, OCI_HTYPE_SERVER);
                        authp = nullptr;
                    }

                    if (errhp != nullptr) {
                        OCIHandleFree((dvoid*)errhp, OCI_HTYPE_ERROR);
                        errhp = nullptr;
                    }
                }
        });

        env->checkErr(errhp, OCIServerAttach(srvhp, errhp, (const OraText*) server.c_str(), server.length(), OCI_DEFAULT));
        env->checkErr(errhp, OCIAttrSet((dvoid*) svchp, OCI_HTYPE_SVCCTX, srvhp, 0, OCI_ATTR_SERVER, (OCIError*) errhp));
        env->checkErr(errhp, OCIAttrSet((dvoid*) authp, OCI_HTYPE_SESSION, (dvoid*) user.c_str(), user.length(), OCI_ATTR_USERNAME, (OCIError*) errhp));
        env->checkErr(errhp, OCIAttrSet((dvoid*) authp, OCI_HTYPE_SESSION, (dvoid*) password.c_str(), password.length(), OCI_ATTR_PASSWORD, (OCIError*) errhp));

        if (sysASM)
            env->checkErr(errhp, OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_SYSASM));
        else
            env->checkErr(errhp, OCISessionBegin(svchp, errhp, authp, OCI_CRED_RDBMS, OCI_DEFAULT));

        env->checkErr(errhp, OCIAttrSet((dvoid*) svchp, OCI_HTYPE_SVCCTX, (dvoid*) authp, 0, OCI_ATTR_SESSION, errhp));

        //DatabaseConnection() finish, no need to recycle
        clean = false;
    }


from openlogreplicator.

bersler avatar bersler commented on May 27, 2024

Fixed with last commit

from openlogreplicator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.