Class: DbSession

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/taps/db_session.rb

Constant Summary collapse

@@connections =
{}
@@mutex =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanupObject

Removes connections that have not been accessed within the past thirty seconds.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/taps/db_session.rb', line 42

def self.cleanup
	@@mutex.synchronize {
		now = Time.now
		@@connections.each do |key, (conn, time)|
			if now - time > 30
				@@connections.delete(key)
				conn.disconnect
			end
		end
	}
end

Instance Method Details

#connectionObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/taps/db_session.rb', line 18

def connection
	@@mutex.synchronize {
		conn =
			if @@connections.key?(key)
				@@connections[key].first
			else
				Sequel.connect(database_url)
			end
		@@connections[key] = [conn, Time.now]
		return conn
	}
end

#disconnectObject



31
32
33
34
35
36
37
38
# File 'lib/taps/db_session.rb', line 31

def disconnect
	@@mutex.synchronize {
		if @@connections.key?(key)
			conn, time = @@connections.delete(key)
			conn.disconnect
		end
	}
end