Hi Wolf-Dieter,
The following class has the methods for creation and use of the query object -
public class QueryHandler
{
private final Map<Integer,PagedQuery> pageQueries =
Collections.synchronizedMap(new HashMap<Integer,PagedQuery>());
public void createPagedQuery(String[] attributes, String condition)
{
PagedQuery pagedQuery = new PagedQuery(attributes, condition);
int queryId = pagedQuery.getQueryId();
pageQueries.put(queryId, pagedQuery);
System.out.println("Created paged query " + queryId);
}
public PageResult getPage(int id, int rows)
{
PagedQuery pagedQuery = pageQueries.get(id);
if (pagedQuery != null)
{
AttributesList page = pagedQuery.getPage(rows);
//return PageResult after processing the page data obtained
}
}
}
And the query class is as follows -
public class PagedQuery
{
protected final Connection conn;
private final String query;
private int retrievedRows;
PagedQuery(String attributes[], String condition) throws DatabaseException
{
Context ctx = new InitialContext();
Datasource dataSource = (DataSource) ctx.lookup("java:Datasources/fmread");
conn = dataSource.getConnection();
query = "SELECT " + attributes + " FROM READ_TABLE WHERE " + condition;
}
AttributesList getPage(int rows) throws DatabaseException
{
AttributesList page = getData(rows);
retrievedRows += page.size();
return page;
}
int getQueryId()
{
return hashCode();
}
public final AttributesList getData(int rows) throws DatabaseException
{
try
{
currentStatement = conn.createStatement();
ResultSet resultSet = currentStatement.executeQuery(query);
//Process resultset to AttributesList and return reqd number of rows
}
catch (SQLException e)
{
throw new DatabaseException("Error executing Oracle query: " +
e.getMessage());
}
}
}
This is not the actual code as I have simplified it for your understanding. The client object first makes a call to createPagedQuery() method of the QueryHandler class and then does one or more getPage() calls based on the number of records desired. It is during the second call that we encounter SQLException most times -
java.sql.SQLException: Connection is not associated with a managed connection.org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5@37408cb8
at org.jboss.resource.adapter.jdbc.WrappedConnection.lock(WrappedConnection.java:81)
at org.jboss.resource.adapter.jdbc.WrappedConnection.createStatement(WrappedConnection.java:170)
at xxxQuery.executeQuery