Class: DbMeta::Oracle::Connection

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/db_meta/oracle/connection.rb

Constant Summary collapse

THREAD_KEY =
:db_meta_oracle_connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#database_instanceObject

Returns the value of attribute database_instance.



10
11
12
# File 'lib/db_meta/oracle/connection.rb', line 10

def database_instance
  @database_instance
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/db_meta/oracle/connection.rb', line 10

def password
  @password
end

#poolObject (readonly)

Returns the value of attribute pool.



11
12
13
# File 'lib/db_meta/oracle/connection.rb', line 11

def pool
  @pool
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/db_meta/oracle/connection.rb', line 10

def username
  @username
end

#workerObject (readonly)

Returns the value of attribute worker.



12
13
14
# File 'lib/db_meta/oracle/connection.rb', line 12

def worker
  @worker
end

Instance Method Details

#disconnectObject



42
43
44
45
46
47
48
# File 'lib/db_meta/oracle/connection.rb', line 42

def disconnect
  release_thread_connection
  return unless @pool
  @pool.destroy
  Log.info("Logged off from #{@username}@#{@database_instance}")
  @pool = nil
end

#getObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/db_meta/oracle/connection.rb', line 21

def get
  unless @pool
    # create connection pool
    @pool = ::OCI8::ConnectionPool.new(1, @worker, 1, @username, @password, @database_instance)
    Log.info("Connected to #{@username}@#{@database_instance}")
  end

  # one logical connection per thread - reused across all fetches in that thread
  Thread.current[THREAD_KEY] ||= ::OCI8.new(@username, @password, @pool)
end

#release_thread_connectionObject



32
33
34
35
36
37
38
39
40
# File 'lib/db_meta/oracle/connection.rb', line 32

def release_thread_connection
  connection = Thread.current[THREAD_KEY]
  return unless connection
  connection.logoff
rescue
  # connection may already be closed
ensure
  Thread.current[THREAD_KEY] = nil
end

#set(username, password, database_instance, worker) ⇒ Object



14
15
16
17
18
19
# File 'lib/db_meta/oracle/connection.rb', line 14

def set(username, password, database_instance, worker)
  @username = username
  @password = password
  @database_instance = database_instance
  @worker = worker
end