Connect DB after instantiating a class

Posted by davidvilla on 29-Jan-2016 13:02

I am trying to connect a DB after a class is initiated and try to access it. But I am not able to. This is what I trying:

prog1.p:
--> connect mydb1
--> call prog2.p

prog2.p:
--> uses class mycls1
--> object for class mycls1

mycls1:
--> access mydb1 (as it is already connected in prog1.p)

Now in mycls1, I want to connect to a new db. So, I tried to call another program that will connect to this new db and then call another program to access the new db. But it does not work that way. 

mycls1:
--> access mydb1
--> call prog3.p

prog3.p:
--> connect mydb2
--> call prog4.p

prog4.p:
--> access mydb2
Here I am not able to access mydb2. 
I also tried to disconnect mydb1 in prog3, but still mydb1 is available in prog4. 

Can you please help?

OE Version: 11.2
OS: Unix

All Replies

Posted by Brian K. Maher on 29-Jan-2016 13:10

prog1.p (no references to any database other than the connect and disconnect statements)
                -> connect to db1
                -> call prog2.p
                                -> use class myclass
                                -> be sure myclass gets cleaned up (no static methods allowed)
                -> return from call to prog2.p
                -> disconnect db1
                -> connect to db2
                -> call prog3.p
                                -> use class myclass
                                -> be sure myclass gets cleaned up (no static methods allowed)
                -> return from call to prog3.p
                -> disconnect db2
 
Alternatively, write the code in the class so only dynamic database objects are used such that running the following returns nothing:
 
rcode-info:file-name = "myclass.r".
display rcode-info:db-references.
 
Brain

Posted by davidvilla on 30-Jan-2016 10:41

I get this. But this is not what I need.

Only in myclass, i will know what db to connect to. And then from there I need to access this db. I cannot come back to prog1 and connect another db. Is that possible?

Posted by Thomas Mercer-Hursh on 30-Jan-2016 13:04

It has always been true that you must connect and disconnect databases one level above where they are used.

Posted by davidvilla on 30-Jan-2016 14:46

I am not sure I am clear on my question.

In prog1.p,

--> connect to mydb1

--> call prog2.p

In prog2.p

--> instantiate class mycls1

(as mydb1 is connected one level up in prog1, mydb1 will be accessible in the methods in mycls1)

In mycls1, I have a situation where I want ro connect to a new db, say newdb2 (in addition to mydb1). I have identified that I have to connect to newdb2 only in a method in mycls1, i.e., in prog2.p. And also it is not the same new db every time. The new db to connect may vary based on the input and is identified dynamically in mycls1 (i.e.,prog2.p).

So, now if I try to use another program to connect to this newdb2 - prog3.p and access newdb2 in another program prog4.p called from prog3.p, I am not able to access it.

In mycls1:

--> method mth1:

       --> identify new db id - newdb2

       --> call prog3.p

In prog3.p

--> connect to new db newdb2

--> call prog4.p

In prog4.p:

--> access newdb2 ------> newdb2 is not available here , eventhough i have connected newdb2 one level up in prog3.p

atleast, this is what i need to do. I have to dynamically identify the db to connect to in a method and connect and access it. 

Any ideas, how this can be done?

Posted by Tim Kuehn on 30-Jan-2016 15:37

For reasons I forget - you need to call a program, connect to the db, and then return from the program before doing anything to access the db.

Posted by davidvilla on 31-Jan-2016 10:33

Sorry, If I am not being clear on my question. I understand everything you guys are saying and I completely follow it. But, my question is not answered yet.

From a class, if I call a .p to connect a db and then call another .p to access the db, it doesn't work.

prog1 (connect db1) --> prog2.p (use class mycls1) --> class mycls1 (access db1) --> prog3.p (connect db2) --> prog4.p (access db2)

Not able to access db2 in prog4.p, eventhough db2 is connected one level up in prog3.p

Is my question clear?

Posted by Tim Kuehn on 31-Jan-2016 15:30

What you're trying to do won't work - you need to return from the procedure that makes the connection before you can run any code that accesses that db. This means you need to follow this structure:

prog1 -> Connect.p

prog1 -> prog2.p (use class mycls1) --> class mycls1 (access db1) -->

prog3.p -> connect.p

prog3.p -> prog4.p (access db2)

Posted by davidvilla on 01-Feb-2016 05:32

Can I make a call to prog3.p from mycls1?

this is the error i get

Database physical names <name> and <name> do not match. No connect. (1018)

You tried to connect two databases with different names at once, using the same logical name for both. Disconnect one of the databases before connecting the other if you need to use the same logical name. Remember that a database disconnect does not take effect until the transaction has terminated, and all the currently executing PROGRESS r-code files that reference that database have completed. " " "

Posted by Brian K. Maher on 01-Feb-2016 05:56

Then you need to use only dynamic database objects in the class.  There is no other way around it.

Posted by Brian K. Maher on 01-Feb-2016 08:14

David,

My previous comment only applies if you want to have an instantiated class instance work against two or more databases.

The 1018 error indicates what the error text detail you posted says .. perhaps your code does the disconnect of the first database while there are still references to that database in which case the disconnect will simply pend until all references to the database are gone.  Make sure there are no running programs (or class instances) which reference DB1 before you disconnect it.

Brian

Posted by davidvilla on 03-Feb-2016 02:37

The problem is that I am not able to disconnect the database once it is instantiated. The db connection lives as long as the class. :(

Posted by Thomas Mercer-Hursh on 03-Feb-2016 09:25

Again, this is expected behavior.  You need to drop back to a higher level.

Posted by davidvilla on 03-Feb-2016 10:31

Yes, I got it.

Thanks all for your insight.

This thread is closed