|
Managed Object Factories |
Top Previous Next |
|
A managed object factory is a special case object that can be used to create objects that need to be tracked over time. Any piece of code in an extension can call a managed object factory and request or return an object to it. An excellent use for managed object factories is accessing a database connection pool. In this case, the connection itself is the managed object and the factory is the pool manager.
See the ConnectionPool example in [installation folder]/examples/ConnectionPool . To use this in your own extension you will need to edit the ConnectionPool's Extension.xml file to provide the correct url, user, and password information for connecting with your database. The database driver will need to go in the extension's lib folder. For example, a mysql database would need both DBPool_v4.8.3.jar and mysql-connector-java-5.0.4-bin.jar in the lib folder, and this section of ConnectionPool's Extension.xml would need to be edited:
<Variable name="poolname" type="string">mysqlpool</Variable> <Variable name="url" type="string">jdbc:mysql://localhost:3306/db</Variable> <Variable name="user" type="string">username</Variable> <Variable name="password" type="string">password</Variable>
Replace the red text with the correct information for your specific database. Merge the ManagedObjects section of the Extension.xml with the rest of the parts needed for your extension.
Any plugin or event handler in the same extension will be able to use lines such as these to connect to the database:
EsObject esDB = new EsObject(); esDB.setString("poolname","mysqlpool"); Connection c = (Connection)getApi().acquireManagedObject("ManagedConnectionPoolExample", esDB);
If you rename the handle for the ManagedObject in Extensions.xml, you will need to edit the above line to match.
|