You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Marco Gattei <ma...@gmail.com> on 2011/11/14 12:55:34 UTC

Getting Current Connection from ObjectContext

Hi everyone....

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

In cayenne 2.0 i was using something like:

static class CountTranslator extends SelectTranslator {

         @Override

         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();

         translator.setQuery(query);

         translator.setAdapter(node.getAdapter());

         translator.setEntityResolver(DataContext.getThreadDataContext().getEntityResolver());

         

         

         

         Connection con = null;

         PreparedStatement stmt = null;

         try {

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

             translator.setConnection(con);

             stmt = translator.createStatement();

             ResultSet rs = stmt.executeQuery();

             if (rs.next()) {

                 return rs.getInt(1);

             }

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

         } catch (Exception e) {

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

         } finally {

             try {

                 if (stmt != null) {

                     stmt.close();

                 }

                 if (con != null) {

                     con.close();

                 }

             } 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

     DataContext.getThreadDataContext().getParentDataDomain().getDataNodes().iterator().next()


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




Re: Getting Current Connection from ObjectContext

Posted by Christian Grobmeier <gr...@gmail.com>.
John,

On Mon, Nov 14, 2011 at 4:23 PM, John Huss <jo...@gmail.com> wrote:
> I have a similar function.  Would there be any interest in adding something
> like this to Cayenne?

it is probably best if you would explain more in detail what exactly
you would like to add to cayenne on the dev@cayenne.apache.org
mailinglist

Here is an interesting link for you, if you would like to contribute
to Cayenne (or any other ASF project):
http://www.apache.org/foundation/getinvolved.html

Cheers
Christian

>
> John
>
> On Mon, Nov 14, 2011 at 6:01 AM, Andrus Adamchik <an...@objectstyle.org>wrote:
>
>> You can get DataDomain directly from ServerRuntime:
>>
>>  DataDomain domain = runtime.getDataDomain();
>>
>> even better - you can get direct access to DataSource:
>>
>>  DataSource ds = runtime.getDataSource("mynodename");
>>
>> So in 3.1 with DI various stack objects are a bit closer to your
>> application.
>>
>> Andrus
>>
>> On Nov 14, 2011, at 2:55 PM, Marco Gattei wrote:
>>
>> > Hi everyone....
>> >
>> >    i'm tryng to write a translator from implementing select count(*)
>> from a Select Query with an Expression built at runtime via
>> ExpressionFactory.
>> >
>> > In cayenne 2.0 i was using something like:
>> >
>> > static class CountTranslator extends SelectTranslator {
>> >
>> >        @Override
>> >
>> >        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();
>> >
>> >        translator.setQuery(query);
>> >
>> >        translator.setAdapter(node.getAdapter());
>> >
>> >
>>  translator.setEntityResolver(DataContext.getThreadDataContext().getEntityResolver());
>> >
>> >
>> >
>> >
>> >        Connection con = null;
>> >
>> >        PreparedStatement stmt = null;
>> >
>> >        try {
>> >
>> >            con = node.getDataSource().getConnection();
>> >
>> >            translator.setConnection(con);
>> >
>> >            stmt = translator.createStatement();
>> >
>> >            ResultSet rs = stmt.executeQuery();
>> >
>> >            if (rs.next()) {
>> >
>> >                return rs.getInt(1);
>> >
>> >            }
>> >
>> >            throw new org.apache.cayenne.CayenneRuntimeException("Count
>> query returned no result");
>> >
>> >        } catch (Exception e) {
>> >
>> >            throw new CayenneRuntimeException("Cannot count", e);
>> >
>> >        } finally {
>> >
>> >            try {
>> >
>> >                if (stmt != null) {
>> >
>> >                    stmt.close();
>> >
>> >                }
>> >
>> >                if (con != null) {
>> >
>> >                    con.close();
>> >
>> >                }
>> >
>> >            } 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
>> >
>> >
>>  DataContext.getThreadDataContext().getParentDataDomain().getDataNodes().iterator().next()
>> >
>> >
>> > 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
>> >
>> >
>> >
>> >
>>
>>
>



-- 
http://www.grobmeier.de

Re: Getting Current Connection from ObjectContext

Posted by John Huss <jo...@gmail.com>.
I have a similar function.  Would there be any interest in adding something
like this to Cayenne?

John

On Mon, Nov 14, 2011 at 6:01 AM, Andrus Adamchik <an...@objectstyle.org>wrote:

> You can get DataDomain directly from ServerRuntime:
>
>  DataDomain domain = runtime.getDataDomain();
>
> even better - you can get direct access to DataSource:
>
>  DataSource ds = runtime.getDataSource("mynodename");
>
> So in 3.1 with DI various stack objects are a bit closer to your
> application.
>
> Andrus
>
> On Nov 14, 2011, at 2:55 PM, Marco Gattei wrote:
>
> > Hi everyone....
> >
> >    i'm tryng to write a translator from implementing select count(*)
> from a Select Query with an Expression built at runtime via
> ExpressionFactory.
> >
> > In cayenne 2.0 i was using something like:
> >
> > static class CountTranslator extends SelectTranslator {
> >
> >        @Override
> >
> >        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();
> >
> >        translator.setQuery(query);
> >
> >        translator.setAdapter(node.getAdapter());
> >
> >
>  translator.setEntityResolver(DataContext.getThreadDataContext().getEntityResolver());
> >
> >
> >
> >
> >        Connection con = null;
> >
> >        PreparedStatement stmt = null;
> >
> >        try {
> >
> >            con = node.getDataSource().getConnection();
> >
> >            translator.setConnection(con);
> >
> >            stmt = translator.createStatement();
> >
> >            ResultSet rs = stmt.executeQuery();
> >
> >            if (rs.next()) {
> >
> >                return rs.getInt(1);
> >
> >            }
> >
> >            throw new org.apache.cayenne.CayenneRuntimeException("Count
> query returned no result");
> >
> >        } catch (Exception e) {
> >
> >            throw new CayenneRuntimeException("Cannot count", e);
> >
> >        } finally {
> >
> >            try {
> >
> >                if (stmt != null) {
> >
> >                    stmt.close();
> >
> >                }
> >
> >                if (con != null) {
> >
> >                    con.close();
> >
> >                }
> >
> >            } 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
> >
> >
>  DataContext.getThreadDataContext().getParentDataDomain().getDataNodes().iterator().next()
> >
> >
> > 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
> >
> >
> >
> >
>
>

Re: Getting Current Connection from ObjectContext

Posted by Andrus Adamchik <an...@objectstyle.org>.
You can get DataDomain directly from ServerRuntime:

  DataDomain domain = runtime.getDataDomain();

even better - you can get direct access to DataSource:

  DataSource ds = runtime.getDataSource("mynodename");

So in 3.1 with DI various stack objects are a bit closer to your application.

Andrus

On Nov 14, 2011, at 2:55 PM, Marco Gattei wrote:

> Hi everyone....
> 
>    i'm tryng to write a translator from implementing select count(*) from a Select Query with an Expression built at runtime via ExpressionFactory.
> 
> In cayenne 2.0 i was using something like:
> 
> static class CountTranslator extends SelectTranslator {
> 
>        @Override
> 
>        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();
> 
>        translator.setQuery(query);
> 
>        translator.setAdapter(node.getAdapter());
> 
>        translator.setEntityResolver(DataContext.getThreadDataContext().getEntityResolver());
> 
>        
>        
>        
>        Connection con = null;
> 
>        PreparedStatement stmt = null;
> 
>        try {
> 
>            con = node.getDataSource().getConnection();
> 
>            translator.setConnection(con);
> 
>            stmt = translator.createStatement();
> 
>            ResultSet rs = stmt.executeQuery();
> 
>            if (rs.next()) {
> 
>                return rs.getInt(1);
> 
>            }
> 
>            throw new org.apache.cayenne.CayenneRuntimeException("Count query returned no result");
> 
>        } catch (Exception e) {
> 
>            throw new CayenneRuntimeException("Cannot count", e);
> 
>        } finally {
> 
>            try {
> 
>                if (stmt != null) {
> 
>                    stmt.close();
> 
>                }
> 
>                if (con != null) {
> 
>                    con.close();
> 
>                }
> 
>            } 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
> 
>    DataContext.getThreadDataContext().getParentDataDomain().getDataNodes().iterator().next()
> 
> 
> 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
> 
> 
> 
>