LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Can't pass database connection reference via Functional Global Variable

Solved!
Go to solution

Hello,

 

I'm working on a large producer/consumer based project, parts of which access a MySQL database. After connecting to the database, I want to store the connection reference inside a functional global variable. Then other parts of the project that need to use it can just read it from the functional globale variable.

 

For some reason, the connection reference isn't valid after it has been read from the functional global variable. It works fine if I wire the reference directly from the connect to the execute query VI.

 

I've attached some VIs that show the problem, but you won't be able to run them unless you have a database to connect to and you hack the query a bit so it selects from your tables.

 

SQL_whole_thing works fine. It connects to the database, returns the information that's requested by the query and then disconnects. No problem.

 

But if I run DB_Connect.vi (which stores the reference in the FGV 'DatabaseID_FGV'), followed by SQL_Query.vi (which reads the reference from the FGV), then I get an error saying that the reference is invalid.

 

Anyone have any idea why this happens?

 

Thanks,

Christian.

 

PS: You'll need DBID_FGVActions.ctl and database_connectioninfo.ctl, also attached

0 Kudos
Message 1 of 3
(3,168 Views)
Solution
Accepted by topic author crnbaker

Your referance on the SR goes "stale" when you implement that way.  Essentially this means that the data in memory at that referance has changed since the referance was stored.  Simply because you changed the data in that location.  this prevents you from passing a refeance to the "old data" and crashing the calling application since the stale referance contains assumptions that are no longer correct about the data.

 

Consider upgrading this FGV to an Action Engine.  This would encapulate all of the Actions that you want to do to the database (Connect, disconnect, fecth, write etc...) and maintain the current referance on the AEs Shift register.  Ben sets out a great case for just this type of functional encapsulation in his famous nugget.  A must read for any programmer


"Should be" isn't "Is" -Jay
Message 2 of 3
(3,158 Views)

Thanks, that makes sense. I'll go ahead with making it an action engine.

 

Christian.

Message 3 of 3
(3,132 Views)