| Class | Ribs::DB |
| In: |
lib/ribs/db.rb
|
| Parent: | Object |
A DB instance represents a specific database configuration, including properties for the Hibernate connection.
| Properties | = | java.util.Properties |
| Environment | = | org.hibernate.cfg.Environment |
| Configuration | = | org.hibernate.cfg.Configuration |
| databases | [RW] | All the defined databases |
| default | [RW] | Is this database the default one? true or false. |
| dialect | [RW] |
The database Hibernate dialect to use. This is currently necessary. The
available choices are:
See the package org.hibernate.dialect at www.hibernate.org/hib_docs/v3/api/ for an explanation of the different dialects. |
| driver | [RW] | Driver for the database connection. Necessary to provide. |
| mappings | [R] | All the mappings that have been defined for this database |
| name | [RW] | Name of the database. Default is :main |
| password | [RW] | The password to connect with. Can be nil |
| properties | [RW] | A hash of properties to pass on to hibernate. Can be any string to string value. |
| uri | [RW] | The JDBC uri of the database. This follows the same rules as Hibernate JDBC URLs. It‘s necessary to provide this. |
| username | [RW] | The username to connect with. Can be nil |
Defines a new database. Takes the name of the database as parameter. There can only be one database with a specific name at any time. The default name is :main. After creating the DB instance, this will be yielded to the block. This block needs to be provided. The db is not registered until after the block has executed.
# File lib/ribs/db.rb, line 20
20: def define(name = :main)
21: db = DB.new(name)
22:
23: yield db
24: register db
25:
26: db.create
27:
28: db
29: end
Gets the named database instance, or the default one if name is nil.
# File lib/ribs/db.rb, line 51
51: def get(name = nil)
52: if name && name != :default
53: self.databases[name]
54: else
55: self.databases.find do |name, db|
56: db.default?
57: end[1]
58: end
59: end
Creates the database with the specific name, or :main if none is provided.
# File lib/ribs/db.rb, line 126
126: def initialize(name = :main)
127: self.name = name
128: self.properties = {}
129: end
Will register a new database, making sure that one and only one database is always the default. The rules are simple for this: If the argument is the only database in the system, it is the default. If there are more databases in the system, and the argument has the default flag set, then all other databases are reset to not be default.
# File lib/ribs/db.rb, line 38
38: def register(db)
39: if self.databases.empty?
40: db.default = true
41: elsif db.default?
42: self.databases.each do |name, db|
43: db.default = false
44: end
45: end
46: self.databases[db.name] = db
47: end
Creates the Hibernate Configuration object and the session factory that provides connections to this database.
# File lib/ribs/db.rb, line 138
138: def create
139: properties = Properties.new
140: properties.set_property(Environment::DIALECT, "org.hibernate.dialect.#{self.dialect}Dialect") if self.dialect
141: properties.set_property(Environment::USER, self.username) if self.username
142: properties.set_property(Environment::PASS, self.password) if self.password
143: properties.set_property(Environment::URL, self.uri) if self.uri
144: properties.set_property(Environment::DRIVER, self.driver) if self.driver
145: self.properties.each do |key, value|
146: properties.set_property(key, value)
147: end
148: @configuration = Configuration.new.add_properties(properties)
149: @configuration.set_interceptor org.jruby.ribs.RubyInterceptor.new(self, (self.default? ? :default : self.name).to_s)
150: @mappings = @configuration.create_mappings
151: reset_session_factory!
152: end
Fetch a new Ribs handle connected to the this database. Returns a Ribs::Handle object.
# File lib/ribs/db.rb, line 162
162: def handle
163: sessions = (Thread.current[:ribs_db_sessions] ||= {})
164: if curr = sessions[self.object_id]
165: curr[1] += 1 #reference count
166: Handle.new(self, curr[0])
167: else
168: sess = @session_factory.open_session
169: sessions[self.object_id] = [sess,1]
170: Handle.new(self, sess)
171: end
172: end
Release a Ribs::Handle object that is connected to this database. That Handle object should not be used after this method has been invoked.
# File lib/ribs/db.rb, line 177
177: def release(handle)
178: res = Thread.current[:ribs_db_sessions][self.object_id]
179: if res[0] == handle.hibernate_session
180: res[1] -= 1
181: if res[1] == 0
182: res[0].close
183: Thread.current[:ribs_db_sessions].delete(self.object_id)
184: end
185: end
186: end