1. Daniel Agudelo
  2. PowerBuilder
  3. Monday, 18 November 2019 19:42 PM UTC

Hello!

guys, i hope that you can to help me. i have a PowerBuilder 2019 application and i'm trying use the SqlCa transaction object outside of the thread principal of the app, these cause inside of the shared object i need to retrieve some data but must be so cause the retrieving time not must be pause the execution time of the application but when the thread of the shared object will going execute the select sentence isn't works, the application always break be and later be close.

someone knows how i can to retrieve any data from database but inside the application and outside the principal thread?

Thanks!

Accepted Answer
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 20 November 2019 02:45 AM UTC
  2. PowerBuilder
  3. # Permalink

Here's the code / sample application of the previous video, see attached to this answer:

All the important code is in the main window, upper command button.

For the multithreading, you'll see that I don't use the same datawindow "d_employee" for the retrieve, but instead "d_employee_no_global_funcs". It's the same datawindow, but without any global function calls in any attribute of the dw. In d_employee, I use "gf_row_back_color" in the columns' background.color attribute. It's just an example.

When I tried to do this the first time in our application, we had a dw with several global function calls in the attributes and I did not get it to work. That's why I use a copy of the dw, but without all the global function calls for the retrieve. The data is then copied into the original dw, using rowscopy(). (so beware, if you have a looooooot of rows, this might not work for you). There's probably better ways of doing this, but right now this is what I've got and I'm a bit rusty.

Ask if you need any help or explanations.

Un saludo desde Reino Unido!

Attachments (1)
Comment
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Monday, 18 November 2019 22:17 PM UTC
  2. PowerBuilder
  3. # 1

You want database interaction on both your main thread and your background thread in parallel (read: simultaneously)  => You need two DB connections => 2 x SQLCA + 2 x SQLDA + 2 x SQLSA.

These are memory areas that you need to either duplicate or perform thread sync like semaphores or mutex locks. PowerScript itself is straightforward but parallel programming is complex.

HTH /Michael

Comment
  1. Daniel Agudelo
  2. Tuesday, 19 November 2019 18:50 PM UTC
Hi Michael,

Yes, i tried to use the same sqlca instance in both threads but the application finally be crashed without show me some error or message, simply be die :/
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Monday, 18 November 2019 22:43 PM UTC
  2. PowerBuilder
  3. # 2

You could also try asynchronous retrieve on a second transaction object, but my experience with multi threading is way better, more stable.

As Michael has said, resumed: you need to connect and disconnect in the shared object to it's own transaction object.

If you're stuck let me know, I have some example somewhere, but have to sleep now (Europe), so that'll be tomorrow.

Comment
  1. Miguel Leeuwe
  2. Wednesday, 20 November 2019 20:39 PM UTC
Ah .. true. Always positive meaning! In Castellano same thing happens.

So ... You've accepted my answer as a solution, is all working now?
  1. Helpful
  1. Daniel Agudelo
  2. Wednesday, 20 November 2019 22:08 PM UTC
of course Miguel, finally i did could to use multithreading and i'll be taking a video clip for show it to you how it's working now!! i'm thankfull so much!!
  1. Helpful
  1. Miguel Leeuwe
  2. Wednesday, 20 November 2019 23:04 PM UTC
De nada, encantado!
  1. Helpful
There are no comments made yet.
Michael Kramer Accepted Answer Pending Moderation
  1. Tuesday, 19 November 2019 19:58 PM UTC
  2. PowerBuilder
  3. # 3

Hi Daniel, I would really like to help.

Can you share the following code snippet?

  1. The variable declaration for the transaction object you use in your shared object.
    Incl. whether it is global, shared, instance, or local.
  2. The code that creates/assigns a transaction object to this variable.
  3. The code that sets up and connects this transaction object to the databse.
  4. IF you do embedded SQL THEN => Example of embedded SQL that fails
  5. IF you use DataWindow/DataStore THEN => code creating DW/DS; Set-transaction; retrieve data

My gut feeling is one of these places there is a simple typo or misunderstanding or a code line missing. I'm sure both  Miguel and I -- and everyone else reading - hope we fix your issue soon.

Bring it on,
/Michael

Comment
  1. Daniel Agudelo
  2. Tuesday, 19 November 2019 20:23 PM UTC
Hi Michael,

Excuse me please, but i throwed my code into trash because i had to do again, if you can to wait me till tomorrow i would can to make it again and show it to you if do you want it so.
  1. Helpful
  1. Michael Kramer
  2. Tuesday, 19 November 2019 20:26 PM UTC
Sure I can wait - I have too much going on simultaneously. PB community (like this Q&A), current work, looking for new job, open source development, etc.
  1. Helpful
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 20 November 2019 02:36 AM UTC
  2. PowerBuilder
  3. # 4

Okay so here it is, I didn't find my examples, so I've ripped it out from our application at work.

First the video, see the attached file in this answer:

I'm using the demo database of sqlanywhere which comes with PB2017R3, but you can change it to any database you want.

You can see 2 datawindows on the main window. 

The one on the left, I'm retrieving with multiThreading (shared object).

The one on the right is a normal retrieve.

When I first click on the upper command button, the left dw - which has in my demo database more than 120.000 rows (I just duplicated the contents of the employee table a lot of times, after having deleted the primary key) - the multithread retrieve starts. 

Before that one finishes, I click on the lower command button which does a 'normal' retrieve. That one has few rows so finishes early.

You can see that the mt works, because I'm able to click on the lower button, while the retrieve of the first button has not finished yet.

After this answer, I'll upload the sample application made with pb 2017 R3 and give some explanation on the code.

 

Attachments (1)
Comment
There are no comments made yet.
Miguel Leeuwe Accepted Answer Pending Moderation
  1. Wednesday, 20 November 2019 03:06 AM UTC
  2. PowerBuilder
  3. # 5

Gosh! I now see your application is PB2019 .. I could have just left it the way I initially made it, jjajaa! Anyway, you have to replace the connection strings in the n_demoMT_connectservice.of_getConnectionInfo() to be something else as "PB Demo DB V2017R3", probably "PB Demo DB V2019" or "PB Demo DB V2019R2" if you're on the beta.
Once you see it all works, then connect to your real database and do some real slow retrievals.

 

Added info:

I think I got most of the needed information here: 

https://www.powerbuildertv.com/index.php/en/archived-webinars/powerbuilder/162-multithreaded-applications-with-powerbuilder There's a link to download the code and to the presentation by Bruce Armstrong.

Look at the Closequery event of the window. I'm using an instance boolean variable to make sure that you cannot close the window until the multithreading is finished. If not you'd get blow-ups.

Events on the dw_employees control:

retrieveEnd - triggered from the n_callback.of_setdata() function

constructor events of both dw's: to settransobject(sqlca)

window close: destroys, though I'm not sure if that's really necessary.

 

Also: you'll see that I'm doing the retrieve with 2 string arguments, but they're just there for demo purpose. The final Retrieve() which I'm doing in n_shared.of_retrieve() does not use them.

Comment
There are no comments made yet.
Daniel Agudelo Accepted Answer Pending Moderation
  1. Friday, 22 November 2019 15:34 PM UTC
  2. PowerBuilder
  3. # 6

Hi Miguel Leeuwe

i attached the videoclip with my lab that's using multithread to retrieve some information while the final user is typing.

again i want to thank you so much!

Attachments (1)
Comment
  1. Miguel Leeuwe
  2. Friday, 22 November 2019 15:39 PM UTC
No worries!

That looks like a useful and very nice case for usage of the multithreading !!
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.