[jboss-user] [Datasource Configuration] - Re: SQLException encountered while trying to use a DB connection

mou_gc do-not-reply at jboss.com
Mon Jan 21 02:23:59 EST 2013


mou_gc [https://community.jboss.org/people/mou_gc] created the discussion

"Re: SQLException encountered while trying to use a DB connection"

To view the discussion, visit: https://community.jboss.org/message/793156#793156

--------------------------------------------------------------
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 at 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
--------------------------------------------------------------

Reply to this message by going to Community
[https://community.jboss.org/message/793156#793156]

Start a new discussion in Datasource Configuration at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2077]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-user/attachments/20130121/d54fa5e3/attachment-0001.html 


More information about the jboss-user mailing list