Thanks. I read through the first few sections of the Java tutorial several days ago. That helped me understand the Java pieces (a little). It's mostly the setup that's killing me - making sure I have all of my hooks in the right place.
I thought I had this working last night, but I was mistaken. I saw the "Query called with string:" trace, figured I'd made a good connection, and called it a night. Tonight, I realized that, even though I successfully passed the query to my plugin, my code is not actually making a complete connection. The last debug statement from the following code is "Poolname set". I read the ES5 section on Managed Object Factories and looked at the "ConnectionPool" example, but I can't figure out what I'm doing wrong. Plus, the "ConnectionPool" example seems to be handling/creating the connection pool differently than the "DatabaseWithJDBC" example.
From "DatabaseJDBCPlugin.java":
Code:
public EsObject doQuery(String query) throws SQLException {
getApi().getLogger().debug("Query called with string: " + query);
EsObject resultSetObj = new EsObject();
Connection con = null;
try {
getApi().getLogger().debug("trying db stuff");
EsObject esDB = new EsObject();
esDB.setString(POOLNAME, poolname);
getApi().getLogger().debug("Poolname set");
con = (Connection) getApi().acquireManagedObject("ManagedConnectionPool", esDB);
getApi().getLogger().debug("Connection established");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
getApi().getLogger().debug("Statement built");
ResultSet answer = null;
answer = stmt.executeQuery(query);
getApi().getLogger().debug("Query executed");
if (answer != null) {
resultSetObj = resultSetToEsObject(answer);
getApi().getLogger().debug("Results found");
while(answer.next()) {
System.out.println(answer.getString(1) + " " + answer.getString(2));
// Returns the data
// result.getString(1) = first field
// result.getString(2) = second field
}
}
} catch (SQLException ex) {
getApi().getLogger().debug("SQL exception: " + ex.getMessage());
Logger.getLogger(DatabaseJDBCPlugin.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (con != null) {
con.close();
}
return resultSetObj;
}
}
Here's my Extension.xml file. ("testdb" is my database name)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<Extension>
<Name>DatabaseWithJDBC</Name>
<ManagedObjects>
<ManagedObject>
<Handle>ManagedConnectionPool</Handle>
<Type>Java</Type>
<Path>com.electrotank.electroserver5.examples.database.ConnectionPool</Path>
<Variables>
<!-- drivers is a comma separated list of JDBC drivers -->
<Variable name="drivers" type="string">com.mysql.jdbc.Driver,org.apache.derby.jdbc.ClientDriver</Variable>
<Variable name="logfile" type="string">dbpool.log</Variable>
<!-- Create an array of EsObjects. One object represents a connection pool. -->
<Variable name="pools" type="EsObject">
<Entry>
<Variable name="poolname" type="string">mysqlpool</Variable>
<Variable name="url" type="string">jdbc:mysql://localhost:3306/testdb</Variable>
<Variable name="user" type="string">username</Variable>
<Variable name="password" type="string">password</Variable>
<Variable name="timeout" type="integer">2000</Variable>
<Variable name="maxpool" type="integer">20</Variable>
<Variable name="maxconn" type="integer">98</Variable>
<Variable name="expiry" type="integer">5</Variable>
</Entry>
<Entry>
<Variable name="poolname" type="string">derbypool</Variable>
<Variable name="url" type="string">jdbc:derby://localhost:1527/testdb</Variable>
<Variable name="user" type="string">username</Variable>
<Variable name="password" type="string">password</Variable>
<Variable name="timeout" type="integer">2000</Variable>
<Variable name="maxpool" type="integer">20</Variable>
<Variable name="maxconn" type="integer">98</Variable>
<Variable name="expiry" type="integer">5</Variable>
<!-- Optional parameters
<Variable name="maxpool" type="integer">10</Variable>
<Variable name="maxconn" type="integer">0</Variable>
<Variable name="expiry" type="integer">60</Variable>
<Variable name="init" type="integer">10</Variable>
<Variable name="validator" type="string">some java class</Variable>
<Variable name="decoder" type="string">some java class</Variable>
<Variable name="cache" type="boolean">false</Variable>
<Variable name="debug" type="boolean">false</Variable>
<Variable name="prop.property" type="string">jdbc specific properties</Variable>
<Variable name="async" type="boolean">false</Variable>
<Variable name="logfile" type="string">new logfile</Variable>
<Variable name="dateformat" type="string">yyyy-MM-dd hh:mm:ss.SSSS</Variable>
-->
</Entry>
</Variable>
</Variables>
</ManagedObject>
</ManagedObjects>
<Plugins>
<Plugin>
<Handle>DatabaseJDBCPlugin</Handle>
<Type>Java</Type>
<Path>com.electrotank.electroserver5.examples.database.DatabaseJDBCPlugin</Path>
<Variables>
<Variable name="poolname" type="string">mysqlpool</Variable>
</Variables>
</Plugin>
<Plugin>
<Handle>TestDatabaseJDBCPlugin</Handle>
<Type>Java</Type>
<Path>com.electrotank.electroserver5.examples.database.TestDatabasePlugin</Path>
</Plugin>
</Plugins>
</Extension>
My server log shows:
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - using database pool: mysqlpool
. . .
INFO DisplayLogger -
-=-=-=-=-=-=-=-
ElectroServer has started successfully
. . .
When I pass a query from my client, I get this in my server log:
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - Query called with string: SELECT now()
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - trying db stuff
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - Poolname set
DEBUG Extensions.DatabaseWithJDBC.Plugins.TestDatabaseJD BCPlugin - {EsObject:
}
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - Query called with string: select * from names_table
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - trying db stuff
DEBUG Extensions.DatabaseWithJDBC.Plugins.DatabaseJDBCPl ugin - Poolname set
So, it's failing on:
con = (Connection) getApi().acquireManagedObject("ManagedConnectionPo ol", esDB);