So how would you recommend to do if I would like to store a data in the gateway (in my case - select a database connection) in module A, and my module B will work closely with module A such that it'll also be reading the stored data from module A (but it won't be updating the stored value, the stored value will be updated from module A only)
I am currently using persistent record in module A to store such data hence why I am trying to access the persistent record from module B, but is there a better way of doing so? Or should I just redefine the persistent record in module B and access it that way?
I'm confused about your use-case. What kind of data are you talking about? Persistent records are for your modules' configuration data, not any kind of runtime data. Runtime data should be routed through your gateway's datasource interface, not via persistent records.
My module will be working with a database which the user will have the set a database connection up from the gateway, and from the module the user can select a database that they want to work with from the list of available database connections.
I get that part. You don't need any persistent records for that. What are you trying to store in the persistence interface? Why would your module B need to access module A's configuration in record form, instead of including in module A some method of delivering that information as a simple java object? (Configuration data!)
I want to store the database name of the database that the user select to connect to (From the list of database connections). So that in module A and B I can work with the selected database. The nice thing with using persistent record is that the data will still be stored persistently through module upgrades (as I want the user the select the database once and not having to worry about it anymore in most cases). I am also making use of the record change listener to listen to changes to the selected database so other parts of my module can react accordingly when the database selection has been changed.
Not too sure what you meant by the configuration data here, could you elaborate a little more please.
Per user? Or for the entire site? If the latter, it is reasonable to be stored in a persistent record. Otherwise it is runtime data that should be stored in a DB connection specified in the designer (probably best to be the default DB of the project).
But either way, your module A gateway hook should simply expose an instance method that delivers the desired information to all callers. The module B boundary class can simply call that method to obtain the current setting any time it needs it. No need to access A's persistent record from module B.
(Is this all related to having a UI component do its own queries? You seems to have the poster child for why that is a bad idea.)
Is it possible to have module B being notified for the change to the persistent record (like what the listener does) from module A to have a more event-driven approach rather than polling?
You could define a listener interface in module A, have B implement it, and register itself with module A using an appropriate method. Then module A can relay change notifications to interested modules.