Hi Koen.

> What we usually do is store this information in the specialized
> WApplication object,
> and then provide a static method MyApplication::instance() which
> returns this MyApplication *object like this:
>
> MyAppliation *MyApplication::instance() {
>  return dynamic_cast<WApplication *>(WApplication::instance());
> }
>
> (Internally, WApplication::instance() does indeed use thread specific
> storage to store the session that is bound to the current thread. Note
> that this may change arbitrarily between events).
>
> In this way you could write:
>
> MyWidget::foo() {
>  Memcache mc(MyApplication::instance()->mc_pool());
>  std::string val = mc.get("my_key");
>  // Do something with val and mc is automatically returned to the pool
> }
>
> without needing to store information in the widgets.
>
> Would this approach not meet your needs ?


Almost.  What I'd like is the following:


void MyApp::foo() {
   MyApp::instance()->req_resource<soci_conn> = MyApp::instance()- 
 >soci_conn();
   soci::session *sql = MyApp::instance()->req_resource<soci_conn>;

   sql.begin();

   MyModel obj;
   obj.do_something();

   sql.commit();
}


and in MyModel:


void MyModel::do_something() {
   soci::session *sql = MyApp::instance()->req_resounrce<soci_conn>;

   sql << "SELECT foo, bar FROM baz", into(foo_), into(bar_);
}


Because of the call to begin()/commit(), it is very important that I  
preserve the exact same SQL session as I descend from the controller  
to the model.  Because of the state-hiding nature of Wt, I don't know  
how to ensure that my database connection gets handed back to the  
connection pool at the end of the HTTP request.  Thoughts on how best  
to achieve this?  -sc



PS: For the sake of providing a complete example for the search  
engines in the future, here's a reasonably complete example for  
someone else to follow (note the correction in dynamic_cast):

#include "MyRes"

class MyApp : public Wt::WApplication {
public:
   MyApp(MyRes& res, const Wt::WEnvironment& env) :  
Wt::WEnvironment(env), res_(res) {}

   MyRes& res() const { return res_; }

   static MyApp *instance() { return dynamic_cast<MyApp  
*>(Wt::WApplication::instance()); }

private:
   MyRes& res_;
};


void MyWidget::some_func() {
   MyRes r(MyApp::instance()->res());
   r.foo();
}



--
Sean Chittenden
[email protected]




------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have 
the opportunity to enter the BlackBerry Developer Challenge. See full prize 
details at: http://p.sf.net/sfu/blackberry
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to