Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Connection and transaction

Connection and transaction

Scheduled Pinned Locked Moved C#
databasequestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mpavas
    wrote on last edited by
    #1

    Hello, I have a global connection object. I have few methods, each of them do database operations, each of the method use one connection object. In order to use the transactions do I need to use a global command object too? class operation { OracleConnection oCon = new OracleConnection(); MethoodA() MethoodB() MethoodC() } MethodA() { Command object is used for database operation } Will this Command Object will also be a global variable access to all the Methods?

    Regards, Pavas

    M 1 Reply Last reply
    0
    • M mpavas

      Hello, I have a global connection object. I have few methods, each of them do database operations, each of the method use one connection object. In order to use the transactions do I need to use a global command object too? class operation { OracleConnection oCon = new OracleConnection(); MethoodA() MethoodB() MethoodC() } MethodA() { Command object is used for database operation } Will this Command Object will also be a global variable access to all the Methods?

      Regards, Pavas

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Why global connection? Just have the methods create the connection as an when needed

      M 1 Reply Last reply
      0
      • M musefan

        Why global connection? Just have the methods create the connection as an when needed

        M Offline
        M Offline
        mpavas
        wrote on last edited by
        #3

        Its a Windows service. And it does lot of operation hence I created a global connection. Also I need to implement the Transactions between different methods which do different operation on database. Please suggest what can be the good approach

        Regards, Pavas

        M 1 Reply Last reply
        0
        • M mpavas

          Its a Windows service. And it does lot of operation hence I created a global connection. Also I need to implement the Transactions between different methods which do different operation on database. Please suggest what can be the good approach

          Regards, Pavas

          M Offline
          M Offline
          musefan
          wrote on last edited by
          #4

          ok i see why you went for a global then, cant say if that is still the best option but i dont know what exactly you are doing. What difference do the methods have, do they need different commands? if so you would change the global command with each method, so may as well just create the command you need in the method and pass to connection

          M 1 Reply Last reply
          0
          • M musefan

            ok i see why you went for a global then, cant say if that is still the best option but i dont know what exactly you are doing. What difference do the methods have, do they need different commands? if so you would change the global command with each method, so may as well just create the command you need in the method and pass to connection

            M Offline
            M Offline
            mpavas
            wrote on last edited by
            #5

            each of the method have different sql operations and they insert records in the database. My question is, I want an approch to implement the transaction.

            Regards, Pavas

            B 1 Reply Last reply
            0
            • M mpavas

              each of the method have different sql operations and they insert records in the database. My question is, I want an approch to implement the transaction.

              Regards, Pavas

              B Offline
              B Offline
              Ben Fair
              wrote on last edited by
              #6

              All you need is the Connection and Transaction objects. You create the Transaction from the Connection, something like:

              DbTransaction tran = conn.BeginTransaction();

              Then, you can do everything off of these objects (create the commands from the connection, etc.); be sure to set the command's Transaction property approriately; so you'd have something like:

              DbConnection conn = GetConnection();
              DbTransaction tran = conn.BeginTransaction();
              bool success = true;
              ...
              success = MethodA(conn, tran);
              success = success && MethodB(conn, tran);
              success = success && MethodC(conn, tran);
              if(!success)
              tran.Rollback();
              else
              tran.Commit();
              ...

              private bool MethodA(DbConnection conn, DbTransaction tran)
              {
              bool result = true;
              DbCommand comm = conn.CreateCommand();
              comm.CommandText = ...
              comm.Transaction = tran;
              ...
              return result;
              }
              ...
              private bool MethodB(DbConnection conn, DbTransaction tran)
              ...
              private bool MethodC(DbConnection conn, DbTransaction tran)
              ...

              If you want to make the Connection and Transaction objects class-level variables and have the methods access them that way instead of passing them in, is up to you; I just prefer this way.

              Keep It Simple Stupid! (KISS)

              M 1 Reply Last reply
              0
              • B Ben Fair

                All you need is the Connection and Transaction objects. You create the Transaction from the Connection, something like:

                DbTransaction tran = conn.BeginTransaction();

                Then, you can do everything off of these objects (create the commands from the connection, etc.); be sure to set the command's Transaction property approriately; so you'd have something like:

                DbConnection conn = GetConnection();
                DbTransaction tran = conn.BeginTransaction();
                bool success = true;
                ...
                success = MethodA(conn, tran);
                success = success && MethodB(conn, tran);
                success = success && MethodC(conn, tran);
                if(!success)
                tran.Rollback();
                else
                tran.Commit();
                ...

                private bool MethodA(DbConnection conn, DbTransaction tran)
                {
                bool result = true;
                DbCommand comm = conn.CreateCommand();
                comm.CommandText = ...
                comm.Transaction = tran;
                ...
                return result;
                }
                ...
                private bool MethodB(DbConnection conn, DbTransaction tran)
                ...
                private bool MethodC(DbConnection conn, DbTransaction tran)
                ...

                If you want to make the Connection and Transaction objects class-level variables and have the methods access them that way instead of passing them in, is up to you; I just prefer this way.

                Keep It Simple Stupid! (KISS)

                M Offline
                M Offline
                mpavas
                wrote on last edited by
                #7

                This is of great help. I was looking for an approch like this.

                Regards, Pavas

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups