Skip to Content
0

Using PowerBuilder Classic .NET assemblies From Multi-Threaded .NET Environments

Aug 16, 2017 at 05:41 PM

62

avatar image
Former Member

Hi,

We're in the process of refactoring a large PowerBuilder application with the goal of keeping our business logic in PB and exposing it as an API for .NET developers by using the .NET assembly target in PowerBuilder 12.6 Classic.

The environments we need to support include multi-threaded environments like IIS and xUnit. In using xUnit (which defaults to running tests in parallel), we've encountered an issue where database connections made on separate threads are interfering with each other. We've also reproduced this issue from a multi-threaded .NET console application.

We've created a very simple .NET assembly that just exposes a connection nvo that wraps a native PB transaction stored as an instance variable in the class. We are not using SQLCA or any global variables. The connection object just has connect and disconnect methods. We import this assembly into a .NET xUnit project, create three separate test classes where each runs a test that instantiates a local connection instance and loops ten times, just connecting and disconnecting with each iteration.

If we force xUnit to run tests serially (see how below), we can repeatedly run all tests without issue. If we let xUnit parallelize the execution of the three test classes, we get random errors. Sometimes after connecting we'll get a "transaction not connected" exception when immediately trying to disconnect or when attempting to connect we'll get a "transaction already connected" exception. Sometimes everything works fine.

We've tried this with different database interfaces - SNC, ODBC, ADO.Net - and they all exhibit the same behavior.

I guess our first question is: this should be possible correct? I'd imagine others have successfully used PB Classic's .NET assemblies from within an environment like IIS?

Assuming this should be possible...

I've attached the two main source files (unfortunately I cannot just upload the entire project as a ZIP file because of upload restrictions here). n_connection.sru is an SRU file that just gets imported into a PB 12.6 Classic .NET assembly target. Tests.cs is the xUnit test file that just gets added to an xUnit v2.2 project that references the assembly created by PB. The app.config should be added to the xUnit project.

Here are the files:

n-connectionsru.txt

testscs.txt

appconfig.txt

There is a static ConnectionInfo class where database server and credentials are specified.

Note that on a fast machine the issue is hard to reproduce. I had to use a program called Prime95 to slow down my machine enough to be able to see the issue. I just repeatedly clicked the "Run All" button in VS's Test Explorer to get the behavior to surface.

Thanks for any insight or help you can give.

Tom

To get xUnit to run tests serially
----------------------------------------------

This took a bit of effort to figure out, so I'll include how we did it here (turns out it's not as simple as unchecking the "Run Tests in Parallel" button in the VS test explorer). I've attached an app.config file that needs to be added to the xUnit project. In there are three lines that are commented out. If you uncomment them, it will force xUnit to run tests serially.

testscs.txt (2.1 kB)
appconfig.txt (498 B)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers