On Wed, Jun 6, 2012 at 4:17 AM, Mateusz Loskot <[email protected]> wrote:
> On 5 June 2012 09:26, Hosanna Higher Technologies Support
> <[email protected]> wrote:
>> Then in destructor I call the close method
>>
>> m_session.close();
>>
>> But When I use the class few times (creating and destroying objects of this
>> class) I find that soci fails to connects to MySQL server with error too
>> many connections. I'm confused since I call the close method on destructor,
>> I suppose that soci should release the connection.
>
> The session::close destroys the concrete session object, namely
> mysql_session_backend in your case.
> Destructor of mysql_session_backend calls mysql_close and
> releases the connection:
>
> void mysql_session_backend::clean_up()
> {
>    if (conn_ != NULL)
>    {
>        mysql_close(conn_);
>        conn_ = NULL;
>    }
> }
>
> So, in theory it looks OK, but...
>
> Could you check if you get the mysql_close called at all?
> I don't have MySQL instance to test.

Seems to me soci is doing the right thing.  I just tried the following
two stupid tests (didn't actually check them in):

void test10()
{
    for (int i = 0; i < 1000000; i++)
    {
        session sql(backEnd, connectString);
    }

    std::cout << "test 10 passed" << std::endl;
}

void test11()
{
    session sql;
    for (int i = 0; i < 1000000; i++)
    {
        sql.open(backEnd, connectString);
        sql.close();
    }
    std::cout << "test 11 passed" << std::endl;
}

and they happily complete.  I added some debug messages in
src/backends/mysql/session.cpp and indeed the sequence mysql_init(),
mysql_real_connect(), mysql_close() is called once per each iteration
of the loops above.

So I'm guessing the problem is elsewhere in the code or in the server
configuration (note that the "Too many connections" error means there
were too many connections total to a given mysql server, not too many
connections from your client.)

> There is one more thing, the constructor of mysql_session_backend
> calls mysql_init,
> so every time you create new session object, mysql_init gets called.
> I'm not experienced with MySQL, but according to this discussion
> "init will create a new connection" thus mysql_init should be called once

mysql_init() doesn't create a connection, it allocates memory for the
MYSQL object (it couldn't create a connection since you don't even
give it the host name, port, etc.),  it has to be called more than
once if you want to create more than one connection in a program
(which might be not a good practice but I never heard that it's
disallowed.)

Thanks,

Aleksander

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to