Overview
Features
Download
Documentation
Community
Add-Ons & Services

Poco::Data::Session global usage

Please post support and help requests here.

Poco::Data::Session global usage

Postby crioto » 09 Jun 2009, 09:49

Hi all. I need to use one opened session in all my app, but i can't understand how to do it.
I'll explain my problem:
Somewhere in my code(probably in one of initialization methods) i'm opening session using ODBC. From now on, i wish to use this session and cast states to it in another classess and methods. When i try to return Poco::Data::Session object - my compiler says that he can't access private data. So what the main point of session usage? Or i need to open new session every time i want to send query to my database?

Sorry for my english ^_^
crioto
 
Posts: 16
Joined: 09 Jun 2009, 09:06
Location: Kyrgyzstan, Bishkek

Re: Poco::Data::Session global usage

Postby alex » 09 Jun 2009, 19:39

crioto wrote:Or i need to open new session every time i want to send query to my database?

No you don't.
crioto wrote:Sorry for my english

No need to apologize - your English is fine. Now post some C++ so we can understand what your problem exactly is.
alex
 
Posts: 1158
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Data::Session global usage

Postby crioto » 10 Jun 2009, 05:13

Thank you, alex!
So, there is some pseudo code

Code: Select all
class A
{
void initDatabaseConnection()
{
// there i'm opening the ODBC session
Poco::Data::ODBC::Connector::registerConnector();
   Sesssion mSession("ODBC", "Driver={SQL Server};Server=xx;Database=xx;Uid=xx;Pwd=xx;");
}
};

class B
{
void someMethod()
{
// i need to use session opened in "class A" here, but how?
}
};
crioto
 
Posts: 16
Joined: 09 Jun 2009, 09:06
Location: Kyrgyzstan, Bishkek

Re: Poco::Data::Session global usage

Postby alex » 10 Jun 2009, 22:52

crioto wrote:So, there is some pseudo code

I was hoping that you would post code that does not compile. Looking at your pseudo code, it seems that your problem is on the programming language level.

Code: Select all
// i need to use session opened in "class A" here, but how?

There is no way, because the session is gone after the initDatabaseConnection() executes. Look up the C++ memory model - in particular the difference between stack and heap.

Here is a "bare bone" example (there are better ways, but just to keep things simple).
Code: Select all
class A {
public:
  A()  {
    Poco::Data::ODBC::Connector::registerConnector();
    _pSSession = new Session("ODBC", "Driver={SQL Server};Server=xx;Database=xx;Uid=xx;Pwd=xx;");
  }

  ~A() {
    delete _pSession;
  }

  Session& session()  {
    return *_pSession;
  }

private:
  Session* _pSession;
};

class B {
  void someMethod()  {
    A a;
    Session session(a.session());
    session << "SELECT * FROM TABLE", now;
  }
};
alex
 
Posts: 1158
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Poco::Data::Session global usage

Postby crioto » 11 Jun 2009, 13:17

alex, thanks again. Yeah, that was my language-level problem, i know it. Your example is working well for me =)

I have one another question about statement execution. Actually, i get run-time error, when try to execute any statement

There is my code:
Code: Select all
// Before executing this code i have the same part of code, but with another SQL-query(procedure execution), and it's work fine
Poco::Data::Statement mQuery(this->getSession());
mQuery << "SELECT UserName, UserDescriptions FROM tblUsers WHERE (UserId=" << id << ");";   
// Actualy i've tryed another forms like
// mQuery << "SELECT UserName, UserDescriptions FROM tblUsers WHERE (UserId=:id);", use(id);   
// But it doesn't work for me too
mQuery.execute();

So, on mQuery.execute() i have a run-time error. How to get error description? Do i need catch it?

One more interesting thing: When i'm trying to debug, i have the next issue(?) in tree.
Code: Select all
+      _Stringbuffer   {_Seekhigh=0x013d7ee0 "SELECT UserName, UserDescriptions FROM tblUsers WHERE (UserId=1);НННННННННННННННННННННННННННННННээээ««««««««оюою" _Mystate=5 _Al={...} }   std::basic_stringbuf<char,std::char_traits<char>,std::allocator<char> >

As you can see, i have some strange symbols after my query, so what is that? It is notmal?
crioto
 
Posts: 16
Joined: 09 Jun 2009, 09:06
Location: Kyrgyzstan, Bishkek

Re: Poco::Data::Session global usage

Postby guenter » 11 Jun 2009, 13:40

Remove the trailing semicolon from the SQL statement. Also, instead of ':id' you should use '?', as some database drivers have issues with the ':id' placeholder format.

The following should work:
Code: Select all
mQuery << "SELECT UserName, UserDescriptions FROM tblUsers WHERE UserId = ?", use(id);


Regarding your second question: strange symbols after your string are okay. std::string's internal buffer is not null-terminated (it stores the actual length), so in the debugger you see everything that's left over in the memory region holding the buffer.
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco::Data::Session global usage

Postby crioto » 11 Jun 2009, 16:49

Thank you for reply, but without semicolon i have the same error.
And one my question still opened - how to get information of this error?
crioto
 
Posts: 16
Joined: 09 Jun 2009, 09:06
Location: Kyrgyzstan, Bishkek

Re: Poco::Data::Session global usage

Postby guenter » 11 Jun 2009, 17:04

Code: Select all
try
{
    mQuery << "SELECT UserName, UserDescriptions FROM tblUsers WHERE UserId = ?", use(id), now;
}
catch (Poco::Exception& exc)
{
   std::cout << exc.displayText() << std::endl;
}
guenter
 
Posts: 1165
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco::Data::Session global usage

Postby crioto » 12 Jun 2009, 07:13

Ok, i realy don't know what's happening, but no one my query can be executed excepting first one.

This code working well
Code: Select all
Poco::Data::Statement mQuery(Core::getSingletonPtr()->getSession());
mQuery << "exec dbo.auth '" << login << "', '" << password << "';";
mQuery.execute();


After these code i'm starting up another method, that contain:
Code: Select all
        try
   {
                Poco::Data::Statement mQuery(Core::getSingletonPtr()->getSession());
      mQuery << "SELECT * FROM tblUsers";
                mQuery.execute();
   }
   catch (Poco::Exception &e)
   {
      MessageBoxA(NULL, e.message().c_str(), e.name(), MB_OK);
   }

And i get exception with name ODBC Handle Error, and without any body-text. So, all the night i spend in storming this code, but without any results
crioto
 
Posts: 16
Joined: 09 Jun 2009, 09:06
Location: Kyrgyzstan, Bishkek

Re: Poco::Data::Session global usage

Postby alex » 12 Jun 2009, 12:46

crioto wrote:And i get exception with name ODBC Handle Error, and without any body-text. So, all the night i spend in storming this code, but without any results

Unofrtunately, the detailed error message is not propagated up the exception hierarchy in 1.3.5.

Catch StatementException and retrieve toString():

Code: Select all
try {
//...
}catch (StatementException& ex) {
 std::cout << ex.toString();
}

The propagation of detailed error description has been fixed in the trunk.

In order to catch the exception, make sure to compile your application with WIN32 defined as explained in this post.
alex
 
Posts: 1158
Joined: 11 Jul 2006, 16:27
Location: United_States

Next

Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests

cron