Method: Spider::Model::Storage::ConnectionPool#get_connection

Defined in:
lib/spiderfw/model/storage/connection_pool.rb

#get_connectionObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 35

def get_connection
    if Spider.conf.get('storage.shared_connection')
        @shared_conn ||= _checkout
        return @shared_conn
    end
    Thread.current[:storage_connections] ||= {}
    Thread.current[:storage_connections][storage_type] ||= {}
    @connection_mutex.synchronize do
        #Spider.logger.debug("DB Pool (#{Thread.current}): trying to get connection")
        if conn = Thread.current[:storage_connections][storage_type][@connection_params]
            #Spider.logger.debug("DB Pool (#{Thread.current}): returning thread connection #{conn}")
            @free_connections.delete(conn)
            conn
        else
            conn = _checkout
            Thread.current[:storage_connections][storage_type][@connection_params] = conn
            @thread_connections[Thread.current.object_id] = [conn, Time.now]
            conn
        end
    end
end