web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Difference between Connection Class and UserConnection Class for connecting to AX database to run stored procedure

(0) ShareShare
ReportReport
Posted on by 565

This is a technical question, in msdn, the Connection class is described as follows:

The Connection class establishes a current database session that you can use to execute SQL statements and return results.

The UserConnection class is described as:

The UserConnection class represents an auxiliary connection to the SQL database, based on the same login properties as the main connection.

What is the practical difference between these two classes?

I'm currently trying to determine why certain connections are being 'stranded' in AX 2012 with blocking happening. One recent change made to AX was to implement a report that makes calls to external stored procedures using the connection class. I then close the resultset and statement objects. The pseudo code looks like the following:

try

    //BP Deviation Documented
    sql = strFmt("StoredProc1 %1,%2",_salesId,_lineNum); sqlPerm = new SqlStatementExecutePermission(sql);
    sqlPerm.assert();
    connection = new Connection();
    statement = connection.createStatement();
    sqlres = statement.executeQuery(sql);
    CodeAccessPermission::revertAssert();
    while(sqlres.next())
    {
        var1 = strLwr(sqlres.getString(1));
        var2 = strLwr(sqlres.getString(2));
    }
sqlres.close();
statement.close();
}

*This post is locked for comments

I have the same question (0)
  • NicholasPeterson Profile Picture
    565 on at
    RE: Difference between Connection Class and UserConnection Class for connecting to AX database to run stored procedure

    Thanks for the answer, extremely helpful.

  • Brandon Wiese Profile Picture
    17,788 on at
    RE: Difference between Connection Class and UserConnection Class for connecting to AX database to run stored procedure

    Also, in case my comment about MySQL and DB2 doesn't make sense, I have those setup as Linked servers in SQL Server and can execute 4 part naming and OPENQUERY statements using Connection that work against remote databases.

  • Verified answer
    Brandon Wiese Profile Picture
    17,788 on at
    RE: Difference between Connection Class and UserConnection Class for connecting to AX database to run stored procedure

    The difference between the Connection class and the UserConnection class is that the Connection class executes SQL against the database WITHIN your current session and transaction scope (TTS).  It is a means of executing more complex statements not supported within standard X++ or QueryBuild logic.

    The UserConnection class forms an entirely new connection to the database outside of your sessions and transaction scope.  The best example of the use of a UserConnection is within the Number sequence framework, which draws from a number sequence regardless of whether the main session aborts, failed, throws and exception, etc.  By using a UserConnection and a separate TTS scope, the number sequence draw can commit even if the main session fails.

    In addition to closing your statement and resultset object, you should also set them to null to immediately dereference.  This should also be done with the connection object itself, except that you should use .finalize() since it is an X++ class (like you would use .Dispose() on a COM object).  Here's is a small snip from a class I use every day to connect to external systems from AX (MySQL, DB2, etc.).

    Good luck!

       connection = new Connection();

       try

       {

           connection.ttsbegin();  // connection specific

           // statement and sql code here

           connection.ttscommit();  // connection specific

       }

       catch

       {

           connection.ttsabort();  // connection specific

           throw error(strFmt('@SYS93289'));  // Operation canceled

       }

       connection.finalize();  // destruct

       connection = null;  // dereference

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Community Member Profile Picture

Community Member 4

#2
Guy Terry Profile Picture

Guy Terry 2 Moderator

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans