title: NHibernate - SafeHandle Cannot be Null
body: <p>I am using NHibernate to connect to an Oracle database. Everything was fine until I suddenly started to get the strange <code>Value cannot be null. Parameter name: SafeHandle cannot be null.</code> error. I am not sure what I have changed to get this error and I do not know what exactly it means. Google does not give results too. Below is the full stack trace. Any ideas ?</p>
  
  <p>Error happens when <code>BuildSessionFactory</code> is called:</p>
  
  <pre><code> var x = Fluently.Configure()
          .Database(OracleClientConfiguration.Oracle10
              .ConnectionString(connectionString))
          .Mappings(m =&gt; m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
          .BuildConfiguration();
  
      return x.BuildSessionFactory();
  </code></pre>
  
  <blockquote>
    <p>at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle
    pHandle, Boolean&amp; success)    at
    System.Data.Common.UnsafeNativeMethods.OCIAttrSet(OciHandle trgthndlp,
    HTYPE trghndltyp, Byte[] attributep, UInt32 size, ATTR attrtype,
    OciHandle errhp)    at
    System.Data.OracleClient.TracedNativeMethods.OCIAttrSet(OciHandle
    trgthndlp, Byte[] attributep, UInt32 size, ATTR attrtype, OciHandle
    errhp)    at System.Data.OracleClient.OciHandle.SetAttribute(ATTR
    attribute, String value, OciErrorHandle errorHandle)    at
    System.Data.OracleClient.OracleInternalConnection.OpenOnLocalTransaction(String
    userName, String password, String serverName, Boolean
    integratedSecurity, Boolean unicode, Boolean omitOracleConnectionName)
    at
    System.Data.OracleClient.OracleInternalConnection..ctor(OracleConnectionString
    connectionOptions)    at
    System.Data.OracleClient.OracleConnectionFactory.CreateConnection(DbConnectionOptions
    options, Object poolGroupProviderInfo, DbConnectionPool pool,
    DbConnection owningObject)    at
    System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection
    owningConnection, DbConnectionPool pool, DbConnectionOptions options) 
    at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection
    owningObject)    at
    System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection
    owningObject)    at
    System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
    owningObject)    at
    System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
    owningConnection)    at
    System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection
    outerConnection, DbConnectionFactory connectionFactory)    at
    System.Data.OracleClient.OracleConnection.Open()    at
    NHibernate.Connection.DriverConnectionProvider.GetConnection()    at
    NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
    at
    NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect
    dialect, IConnectionHelper connectionHelper)    at
    NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactory
    sessionFactory)    at
    NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping
    mapping, Settings settings, EventListeners listeners)    at
    NHibernate.Cfg.Configuration.BuildSessionFactory()    at
    Models.SessionFactory.CreateOracleConnection(String connectionString)
    in c:\Users\CMENGU\Projects\TNT\Models\SessionManager.cs:line 21    at
    TNT.SessionManager.InitOracleFactory() in
    c:\Users\CMENGU\Projects\TNT\TNT\SessionManager.cs:line 29    at
    TNT.Program.Main() in c:\Users\CMENGU\Projects\TNT\TNT\Program.cs:line
    30    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,
    String[] args)    at System.AppDomain.ExecuteAssembly(String
    assemblyFile, Evidence assemblySecurity, String[] args)    at
    Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br>
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    preserveSyncCtx)    at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    preserveSyncCtx)    at
    System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state)    at
    System.Threading.ThreadHelper.ThreadStart()</p>
  </blockquote>