Class: Iudex::DA::PoolDataSourceFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/iudex-da/pool_data_source_factory.rb

Overview

Factory for a DataSource using commons-dbcp and postgres driver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_props = {}) ⇒ PoolDataSourceFactory

Returns a new instance of PoolDataSourceFactory.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/iudex-da/pool_data_source_factory.rb', line 38

def initialize( in_props = {} )
  @props = Hooker.merge( [ :iudex, :connect_props ],
                         CONFIG.merge( in_props ) )

  # Tweeks specific for Java datasource/pool
  @props[ :user ] ||= @props[ :username ]
  @props.delete( :username )

  @props[ :loglevel ] ||= 1

  RJack::SLF4J[ 'iudex.da.PoolDataSourceFactory' ].info do
    "Init properties: #{@props.inspect}"
  end
  load_driver
end

Instance Attribute Details

#data_sourceObject

Returns the value of attribute data_source.



36
37
38
# File 'lib/iudex-da/pool_data_source_factory.rb', line 36

def data_source
  @data_source
end

Instance Method Details

#closeObject



60
61
62
63
# File 'lib/iudex-da/pool_data_source_factory.rb', line 60

def close
  @con_pool.close
  @con_pool = @data_source = nil
end

#createObject



54
55
56
57
58
# File 'lib/iudex-da/pool_data_source_factory.rb', line 54

def create
  con_factory = create_connection_factory
  @con_pool = create_connection_pool( con_factory )
  @data_source = PoolingDataSource.new( @con_pool )
end

#create_connection_factoryObject



74
75
76
77
78
79
80
81
# File 'lib/iudex-da/pool_data_source_factory.rb', line 74

def create_connection_factory
  uri = "jdbc:postgresql://%s/%s" % [ @props[ :host ], @props[ :database ] ]

  jprops = Properties.new
  @props.each { |k,v| jprops.set_property( k.to_s, v.to_s ) }

  DriverManagerConnectionFactory.new( uri, jprops )
end

#create_connection_pool(con_factory) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/iudex-da/pool_data_source_factory.rb', line 83

def create_connection_pool( con_factory )
  con_pool = GenericObjectPool.new( nil )

  con_count = @props[ :pool ]
  if con_count
    con_pool.max_active = con_count
    con_pool.max_idle = con_count
  end

  props = @props[ :ds_pool ]
  if props
    props.each { |k,v| con_pool.send( k.to_s + '=', v ) }
  end

  # This sets self on con_pool
  PoolableConnectionFactory.new( con_factory,
                                 con_pool,
                                 nil, #stmtPoolFactory
                                 nil, #validationQuery
                                 false, #read_only_default
                                 true ) #auto_commit_default
  con_pool
end

#load_driverObject



65
66
67
68
69
70
71
72
# File 'lib/iudex-da/pool_data_source_factory.rb', line 65

def load_driver
  import 'org.postgresql.Driver'
  lw = LogWriter.new( 'iudex.da.Driver' )
  # Remove postgres time stamp, trailing whitespace.
  lw.remove_pattern =
    Pattern.compile( '(^\d\d:\d\d:\d\d\.\d\d\d\s\(\d\)\s)|(\s+$)' )
  DriverManager::set_log_writer( PrintWriter.new( lw, true ) )
end