cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marco Gattei <>
Subject Getting Current Connection from ObjectContext
Date Mon, 14 Nov 2011 11:55:34 GMT
Hi everyone....

     i'm tryng to write a translator from implementing select count(*) 
from a Select Query with an Expression built at runtime via 

In cayenne 2.0 i was using something like:

static class CountTranslator extends SelectTranslator {


         public String createSqlString() throws Exception {

             String sql = super.createSqlString();

             int index = sql.indexOf(" FROM ");

             return "SELECT COUNT(*)" + sql.substring(index);



     public int count(SelectQuery query,

             DataNode node) {

         CountTranslator translator = new CountTranslator();







         Connection con = null;

         PreparedStatement stmt = null;

         try {

             con = node.getDataSource().getConnection();


             stmt = translator.createStatement();

             ResultSet rs = stmt.executeQuery();

             if ( {

                 return rs.getInt(1);


             throw new org.apache.cayenne.CayenneRuntimeException("Count query returned no

         } catch (Exception e) {

             throw new CayenneRuntimeException("Cannot count", e);

         } finally {

             try {

                 if (stmt != null) {



                 if (con != null) {



             } catch (Exception ex) {

                 throw new CayenneRuntimeException("Cannot close connection", ex);




the only thing i nedeed was to get the current connection from the 
context which i did using something like


but now in Cayenne 3.1 this method seems to be invalid 'cause i cannot 
get ParentDataDomain from ObjectContext.
Besides that this works only if have a datanode set for the domain.
Is there any other or better way to proceed?

Thanks, Marco

View raw message