SqlPool::SqlConnectionPtr SqlPool::get(const String &source_id, int timeout) { SqlSource src; { ScopedLock lock(pool_mux_); std::map<String, SqlSource>::iterator source_it = sources_.find(source_id); if (sources_.end() == source_it) throw PoolError(_T("Unknown source ID: ") + source_id); if (pools_[source_id].size()) { SqlConnectionPtr handle = pools_[source_id].front(); ++counts_[source_id]; pools_[source_id].pop_front(); LOG(ll_INFO, _T("got connection") + get_stats(source_id)); return handle; } src = source_it->second; } LOG(ll_DEBUG, _T("opening connection") + format_stats(source_id)); SqlConnectionPtr handle; if (interlocked_open_) { ScopedLock lock(open_mux_); handle = new SqlConnection(src); } else handle = new SqlConnection(src); LOG(ll_INFO, _T("opened connection") + get_stats(source_id)); { ScopedLock lock(pool_mux_); ++counts_[source_id]; } return handle; }
SqlConnectionVar::SqlConnectionVar(const SqlPoolDescr &d) : pool_(d.get_pool()) , handle_(pool_.get(d.get_source_id(), d.get_timeout())) { if (!handle_) throw PoolError(_T("Can't get connection")); }
SqlConnectionVar::SqlConnectionVar(SqlPool &pool, const String &source_id, int timeout) : pool_(pool) , handle_(pool_.get(source_id, timeout)) { if (!handle_) throw PoolError(_T("Can't get connection")); }
SqlConnection *Engine::get_from_pool() { if (!pool_.get()) throw PoolError(_T("Engine with no connection")); SqlConnection *conn = pool_->get(source_id_, timeout_); if (!conn) throw PoolError(_T("Can't get connection")); dialect_ = conn->get_dialect(); conn->set_echo(echo_); conn->init_logger(logger_.get()); return conn; }