def initialize(vendor=nil, host='localhost', dbName=nil, userName=nil, password=nil, poolSize=5, monitorInterval=300, &block)
@classMutex = Mutex.new
@connectionFreed = ConditionVariable.new
@instance = nil
@mungeBlock = block ? block : Proc.new {|dbh| dbh}
raise ReInitException if @instance
@classMutex.synchronize {
@vendor = vendor
@host = host
@dbName = dbName
@userName = userName
@password = password
@poolSize = poolSize
@monitorInterval = monitorInterval
@pool = []
0.upto(@poolSize-1) do |i|
conn = @pool[i] = connect
class << conn
attr_accessor :status
attr_accessor :parent
def freeConnection
parent.freeConnection(self)
end
attr_accessor :transaction_depth
def begin_transaction
if @transaction_depth == 0
self.do("BEGIN")
end
@transaction_depth += 1
end
def commit_transaction
if @transaction_depth == 1
self.do("COMMIT")
end
@transaction_depth -= 1
end
def rollback_transaction
if @transaction_depth == 1
self.do("ROLLBACK")
end
@transaction_depth -= 1
end
def transaction
raise InterfaceError, "Database connection was already closed!" if @handle.nil?
raise InterfaceError, "No block given" unless block_given?
begin_transaction
begin
yield self
commit_transaction
rescue Exception
rollback_transaction
raise
end
end
end
conn.status = :IDLE
conn.parent = self
conn.transaction_depth = 0
end
@monitorThread = Thread.new do
while(true)
sleep(monitorInterval)
@classMutex.synchronize {
@pool.each do |conn|
if (conn.status == :IDLE) && (not conn.ping)
conn = connect
end
end
}
end
end
@instance = self
}
end