Subject Re: Jaybird Problem
Author Roman Rokytskyy <rrokytskyy@acm.org>
Hi,

> Last message was not explicit, this is my case:
>
> 1) Build a java class (Bean) which provide access to database, this
> is an example:

Your class is not correct:

a) you should not open a connection from within a web application,
but use DataSources provide by a web server. See Tomcat documentation
and Tomcat-related topics in our FAQ;

b) you should obtain connection right before using and close it right
after using. Web server will provide connection pooling, so this will
be fast.

c) Prepared statements should be closed before connection is closed
(most likely connection pool will close them anyway, but this is more
correct).

If you do not want to use Tomcat's connection pool, you can use
org.firebirdsql.jdbc.FBWrappingDataSource pool. See our FAQ on how to
use it.

Now your class should look like this:

public class SiteData{

private javax.sql.DataSource dbData;

public void openDB(){
/*
Class.forName(JayBirdDriver);
dbData = DriverManager.getConnection(JayBirdURL, "dbUsr", "dbPwd");
dbData.setAutoCommit(false);
*/
javax.naming.Context ctx = new javax.naming.InitialContext();
dbData = (javax.sql.DataSource)ctx.lookup(jaybirdJndiName);

/*
// or something like this
// (check method names, I'm writing them from the memory)
org.firebirdsql.jdbc.FBWrappingDataSource ds
= new org.firebirdsql.jdbc.FBWrappingDataSource();
ds.setDatabase(JayBirdURL);
db.setUser("dbUsr");
db.setPassword("dbPwd");
*/
}
public void insertData(int pData1, String pData2) throws SQLException
{

Connection connection = dbData.getConnection();
PreparedStatement qrData = null;

try {
qrData = connection.prepareStatement("INSERT INTO tbl " +
" VALUES (pData1, pData2)");
qrData.executeUpdate();
dbData.commit();
} finally {
try {
if (qrData != null)
qrData.close();
} finally {
connection.close();
}
}
}

public String[] getData(){
String[] sData;

// similar to the previous method

return sData;
}

}
> ------------------------------------------------------------
>
> 2) JayBird Drivers are installed on Tomcat/lib directory, driver is
> installed for all Tomcat applications.
>
> 3) Under Tomcat/webapps directory exists an application named
> jayBird, which contains 3 jsp pages: welcome.jsp, report.jsp,
> endAppl.jsp. Each page uses SiteData as session bean, following tag
> is included at each page:
>
> <jsp:useBean id="objData" class="SiteData" scope="session" />


Use application scope for this bean. Having connection bound to the
session does not guarantee you that it will be eventually released.
New bean above uses minimum possible number of connections and is
thread safe.

> 4) welcome.jsp calls objData.openDB(), then post (calls) report.jsp
> page.
>
> 5) report.jsp calls objdata.insertData(), objData.getdata()
methods,
> then post (calls) endAppl.jsp.
>
> 6) endAppl.jsp calls objData.closeDB() method, then invalidate
> sesion: session.invalidate();

Closing is no longer needed. Pool will be released when application
is stopped.

Hope this helps.

Best regards,
Roman Rokytskyy