vrogier / ocilib

OCILIB (C and C++ Drivers for Oracle) - Open source C and C++ library for accessing Oracle databases
http://www.ocilib.net
Apache License 2.0
325 stars 119 forks source link

On connection pool problem #343

Closed MagicXran closed 1 year ago

MagicXran commented 1 year ago

Excuse me, I use ocilib::Pool and getConnection () every time. In this case, will the connection be automatically re-established when the resources in the connection pool are exhausted? Is it possible to use getConnection () to get a connection and automatically recycle it after use? This can avoid the shortage of resources in the connection pool and the frequent creation of links when the database is frequently operated.

MagicXran commented 1 year ago

In order to maximize usage, I will execute multiple sql on the same connection. Please tell me, is this possible, or are there any better improvement measures? thanks!

 // initialize  ocilib::Pool pool

function (ocilib::Pool pool)
{
  auto conn = pool.getConnection();
  ocilib::Statement stmt(conn);

  if(condition)
   {
     try
                        {

                            stmt.Execute(sql);
                            conn.Commit();
                        }
                        catch (...)
                        {
                            conn.Rollback();
                            std::cerr << "Error in " << EventName << std::endl;
                        }
}

// Use the same connection
 try
                        {

                            stmt.Execute(sql2);
                            conn.Commit();
                        }
                        catch (...)
                        {
                            conn.Rollback();
                            std::cerr << "Error in " << EventName << std::endl;
                        }
//.....
vrogier commented 1 year ago

Hi,

As soon the connection object is out of scope (destructor invoked) and if there is no more reference to the underlying handle, the oracle connection is returned to the pool automatically. Calling connection::Close() does the same ( in fact the destructor calls Close()).

By default, in case of exhausted pool, Pool::GetConnection() is a blocking call until a connection is available (this behaviour can be changed using Pool::SetNoWait()).

Regards,

Vincent

MagicXran commented 1 year ago

If Pool::SetNoWait(TRUE), what happens when the connection resources are exhausted? Will a new connection resource be created? Or return empty? Or something else

vrogier commented 1 year ago

Pool::GetConnection() will raise an exception

MagicXran commented 1 year ago

Excuse me, is it possible to establish two ocilib::Pool to connect to different databases respectively? Will there be conflicts?

vrogier commented 1 year ago

Of course you can use different pools to connect to different databases :)

Vincent