100% of people found this useful
Alternatively, you can have the ISessionFactory open connections for you. The ISessionFactory must be provided with ADO.NET connection properties in one of the following ways:
-
Pass an instance of IDictionary<string, string> (property name, property value) to Configuration.SetProperties(IDictionary<string, string>).
-
Include <property> elements in a configuration section in the application configuration file. The section should be named hibernate-configuration and its handler set to NHibernate.Cfg.ConfigurationSectionHandler. The XML namespace of the section should be set to urn:nhibernate-configuration-2.2.
-
Include <property> elements in hibernate.cfg.xml (discussed later).
If you take this approach, opening an ISession is as simple as:
ISession session = sessions.OpenSession(); // open a new Session
/* do some data access work, an ADO.NET connection will be used on demand */
All NHibernate property names and semantics are defined on the class NHibernate.Cfg.Environment. We will now describe the most important settings for ADO.NET connection configuration.
NHibernate will obtain (and pool) connections using an ADO.NET data provider if you set the following properties:
NHibernate ADO.NET Properties
<tbody>
| Property name |
Purpose |
| connection.provider_class |
The type of a custom IConnectionProvider.
eg. full.classname.of.ConnectionProvider if the Provider is built into NHibernate, or full.classname.of.ConnectionProvider, assembly if using an implementation of IConnectionProvider not included in NHibernate.
|
| connection.driver_class |
The type of a custom IDriver, if using DriverConnectionProvider.
full.classname.of.Driver if the driver is built into NHibernate, or full.classname.of.Driver, assembly if using an implementation of IDriver not included in NHibernate.
This is usually not needed, most of the time the dialect will take care of setting the IDriver using a sensible default. See the API documentation of the specific dialect for the defaults.
|
| connection.connection_string |
Connection string to use to obtain the connection. |
| connection.connection_string_name |
The name of the connection string (defined in <connectionStrings> configuration file element) to use to obtain the connection. |
| connection.isolation |
Set the ADO.NET transaction isolation level. Check System.Data.IsolationLevel for meaningful values and the database's documentation to ensure that level is supported.
eg. Chaos, ReadCommitted, ReadUncommitted, RepeatableRead, Serializable, Unspecified
|
| connection.release_mode |
Specify when NHibernate should release ADO.NET connections. See Connection Release Modes.
eg. auto (default) | on_close |after_transaction
Note that this setting only affects ISessions returned from ISessionFactory.OpenSession. For ISessions obtained through ISessionFactory.GetCurrentSession, the ICurrentSessionContext implementation configured for use controls the connection release mode for those ISessions. See Contextual Sessions.
|
| command_timeout |
Specify the default timeout of IDbCommands generated by NHibernate. |
| adonet.batch_size |
Specify the batch size to use when batching update statements. Setting this to 0 (the default) disables the functionality. See Batch updates. |
</tbody>
This is an example of how to specify the database connection properties inside a web.config:
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider, NHibernate
</property>
<property name="connection.connection_string">
Server=127.0.0.1; Initial Catalog=thedatabase; Integrated Security=SSPI
</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="current_session_context_class">managed_web</property>
</session-factory>
</hibernate-configuration>
<!-- other app specific config follows -->
</configuration>
NHibernate relies on the ADO.NET data provider implementation of connection pooling.
You may define your own plugin strategy for obtaining ADO.NET connections by implementing the interface NHibernate.Connection.IConnectionProvider. You may select a custom implementation by setting connection.provider_class.