Hi,

use prepareCached instead of prepare then. prepareCached caches the
statement in the connection. The destructor of tntdb::Statement puts
the statement to the statement cache instead of releasing it.

There is also a tntdb::connectCached function, which uses a
connection pool, which might be interesting to you.

Tommi

Am Sat, 14 May 2011 09:48:37 +0200
schrieb Michael Tomuschat <[email protected]>:

> Hi Tommi,
> 
> I see, I made a poor example.
> 
> The point is, that I don't know when the connection is changed and
> during the lifetime of the connection I want to keep the statement
> prepared. That means I can reset a statement not until I see that the
> connectipon has changed. I also tried to create the statement with
> new/delete, but when I do it after I changed the connection, I also
> get a Segmentation Fault.
> 
> ...
>    tntdb::Connection conn;
>    tntdb::Statement* st = NULL;
> 
>    for( int i = 1;; i++) {
>       cp = ( i % 2) ?  "sqlite:testdb_2.db" :
> "postgresql:dbname=test2"; std::cout << "connecting to " << cp <<
> std::endl; //      if( st != NULL)
> //         delete st;
>       conn = tntdb::connect( cp);
>       std::cout << "going to prepare statement" << std::endl;
> 
>       if( st != NULL)
>          delete st;
>       st = new tntdb::Statement();
>       ( *st) = conn.prepare( "UPDATE y SET x = x + 1");
> ...
> 
> so, is it possible to reset a tntdb::Statement to it's initial state?
> Or what else can I do?
> 
> Michael
> 
> Am Freitag 13 Mai 2011 17:30:10 schrieb Tommi Mäkitalo:
> > Hello,
> > 
> > the problem, which cases the bug is the lifetime of the objects. A
> > tntdb::Statement must be released before the tntdb::Connection,
> > where it was prepared. It is not quite obvious here.
> > 
> > The variables conn and st were declared outside the loop. Inside the
> > loop you first create a connection and then prepare the statement.
> > In the next iteration a new connection is created. The old one is
> > released automatically, since it is the only reference. Here the
> > tntdb::Statement is still held. In the next prepare, the previous
> > statement is released, but the corresponding connection do not exist
> > any more.
> > 
> > The solution is to put the tntdb::Statement st inside the loop:
> > 
> >     for( int i = 1;; i++) {
> >        cp = ( i % 2) ?  "sqlite:testdb_2.db" :
> > "postgresql:dbname=test2"; std::cout << "connecting to " << cp <<
> > std::endl; conn = tntdb::connect( cp);
> > 
> >        std::cout << "going to prepare statement" << std::endl;
> >        tntdb::Statement st = conn.prepare( "UPDATE y SET x = x +
> > 1"); std::cout << "going to execute statement" << std::endl;
> >        st.execute();
> > 
> > 
> > Tommi and many greetings from the LinuxTag 2011 in Berlin.
> 


------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to