Esempio n. 1
0
        public static FluentConfiguration ExposeDbCommand(this FluentConfiguration fluentConfiguration, Action <IDbCommand, global::NHibernate.Cfg.Configuration> action)
        {
            fluentConfiguration.ExposeConfiguration(cfg =>
            {
                DbConnection connection      = null;
                IConnectionProvider provider = null;
                IDbCommand command           = null;

                try
                {
                    provider            = GetConnectionProvider(cfg);
                    connection          = provider.GetConnection();
                    command             = connection.CreateCommand();
                    command.CommandType = CommandType.Text;

                    action(command, cfg);
                }
                finally
                {
                    if (command != null)
                    {
                        command.Dispose();
                    }

                    if (connection != null)
                    {
                        provider.CloseConnection(connection);
                        provider.Dispose();
                    }
                }
            });

            return(fluentConfiguration);
        }
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         _connectionProvider.Dispose();
     }
 }
Esempio n. 3
0
        private void InitConnectionAndExecute(Action <string> scriptAction, bool execute, bool justDrop, DbConnection connection, TextWriter exportOutput)
        {
            Initialize();
            TextWriter          fileOutput         = exportOutput;
            IConnectionProvider connectionProvider = null;

            try
            {
                if (fileOutput == null && outputFile != null)
                {
                    fileOutput = new StreamWriter(outputFile);
                }

                if (execute && connection == null)
                {
                    if (_requireTenantConnection)
                    {
                        throw new ArgumentException("When Database multi-tenancy is enabled you need to provide explicit connection. Please use overload with connection parameter.");
                    }

                    var props = new Dictionary <string, string>();
                    foreach (var de in dialect.DefaultProperties)
                    {
                        props[de.Key] = de.Value;
                    }

                    if (configProperties != null)
                    {
                        foreach (var de in configProperties)
                        {
                            props[de.Key] = de.Value;
                        }
                    }

                    connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props);
                    connection         = connectionProvider.GetConnection();
                }

                Execute(scriptAction, execute, justDrop, connection, fileOutput);
            }
            catch (HibernateException)
            {
                // So that we don't wrap HibernateExceptions in HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                log.Error(e, e.Message);
                throw new HibernateException(e.Message, e);
            }
            finally
            {
                if (connectionProvider != null)
                {
                    connectionProvider.CloseConnection(connection);
                    connectionProvider.Dispose();
                }
            }
        }
Esempio n. 4
0
        public async Task ExecuteAsync(Action <string> scriptAction, bool execute, bool justDrop, TextWriter exportOutput, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            await(InitializeAsync(cancellationToken)).ConfigureAwait(false);
            DbConnection        connection         = null;
            TextWriter          fileOutput         = exportOutput;
            IConnectionProvider connectionProvider = null;

            try
            {
                if (fileOutput == null && outputFile != null)
                {
                    fileOutput = new StreamWriter(outputFile);
                }

                if (execute)
                {
                    var props = new Dictionary <string, string>();
                    foreach (var de in dialect.DefaultProperties)
                    {
                        props[de.Key] = de.Value;
                    }

                    if (configProperties != null)
                    {
                        foreach (var de in configProperties)
                        {
                            props[de.Key] = de.Value;
                        }
                    }

                    connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props);
                    connection         = await(connectionProvider.GetConnectionAsync(cancellationToken)).ConfigureAwait(false);
                }

                await(ExecuteAsync(scriptAction, execute, justDrop, connection, fileOutput, cancellationToken)).ConfigureAwait(false);
            }
            catch (OperationCanceledException) { throw; }
            catch (HibernateException)
            {
                // So that we don't wrap HibernateExceptions in HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                log.Error(e, e.Message);
                throw new HibernateException(e.Message, e);
            }
            finally
            {
                if (connection != null)
                {
                    connectionProvider.CloseConnection(connection);
                    connectionProvider.Dispose();
                }
            }
        }
Esempio n. 5
0
        public void Execute(Action <string> scriptAction, bool export, bool justDrop)
        {
            Initialize();
            IDbConnection       connection         = null;
            StreamWriter        fileOutput         = null;
            IConnectionProvider connectionProvider = null;

            var props = new Dictionary <string, string>();

            foreach (var de in dialect.DefaultProperties)
            {
                props[de.Key] = de.Value;
            }

            if (configProperties != null)
            {
                foreach (var de in configProperties)
                {
                    props[de.Key] = de.Value;
                }
            }

            try
            {
                if (outputFile != null)
                {
                    fileOutput = new StreamWriter(outputFile);
                }

                if (export)
                {
                    connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props);
                    connection         = connectionProvider.GetConnection();
                }

                Execute(scriptAction, export, justDrop, connection, fileOutput);
            }
            catch (HibernateException)
            {
                // So that we don't wrap HibernateExceptions in HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                log.Error(e.Message, e);
                throw new HibernateException(e.Message, e);
            }
            finally
            {
                if (connection != null)
                {
                    connectionProvider.CloseConnection(connection);
                    connectionProvider.Dispose();
                }
            }
        }
Esempio n. 6
0
        protected virtual void Dispose(bool isDisposing)
        {
            if (!isDisposing)
            {
                return;
            }
            _disposeCalled = true;

            // We cannot set connection pool to be null,
            // otherwise we might get NPE when using concurrently with NewSession
            _connectionProvider.Dispose();
            _logger = null;
        }
Esempio n. 7
0
 private void dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             if (_connectionProvider != null)
             {
                 _connectionProvider.Dispose();
                 _connectionProvider = null;
             }
         }
         _disposed = true;
     }
 }
Esempio n. 8
0
 public void Dispose()
 {
     _connectionProvider.Dispose();
 }
        /// <summary>
        /// Executes the Export of the Schema.
        /// </summary>
        /// <param name="script"><c>true</c> if the ddl should be outputted in the Console.</param>
        /// <param name="export"><c>true</c> if the ddl should be executed against the Database.</param>
        /// <param name="justDrop"><c>true</c> if only the ddl to drop the Database objects should be executed.</param>
        /// <param name="format"><c>true</c> if the ddl should be nicely formatted instead of one statement per line.</param>
        /// <remarks>
        /// This method allows for both the drop and create ddl script to be executed.
        /// </remarks>
        public void Execute(bool script, bool export, bool justDrop, bool format)
        {
            IDbConnection       connection         = null;
            StreamWriter        fileOutput         = null;
            IConnectionProvider connectionProvider = null;
            IDbCommand          statement          = null;

            IDictionary props = new Hashtable();

            foreach (DictionaryEntry de in dialect.DefaultProperties)
            {
                props[de.Key] = de.Value;
            }

            if (connectionProperties != null)
            {
                foreach (DictionaryEntry de in connectionProperties)
                {
                    props[de.Key] = de.Value;
                }
            }

            try
            {
                if (outputFile != null)
                {
                    fileOutput = new StreamWriter(outputFile);
                }

                if (export)
                {
                    connectionProvider = ConnectionProviderFactory.NewConnectionProvider(props);
                    connection         = connectionProvider.GetConnection();
                    statement          = connection.CreateCommand();
                }

                for (int i = 0; i < dropSQL.Length; i++)
                {
                    try
                    {
                        string formatted;
                        if (format)
                        {
                            formatted = Format(dropSQL[i]);
                        }
                        else
                        {
                            formatted = dropSQL[i];
                        }

                        if (delimiter != null)
                        {
                            formatted += delimiter;
                        }
                        if (script)
                        {
                            Console.WriteLine(formatted);
                        }
                        if (outputFile != null)
                        {
                            fileOutput.WriteLine(formatted);
                        }
                        if (export)
                        {
                            statement.CommandText = dropSQL[i];
                            statement.CommandType = CommandType.Text;
                            statement.ExecuteNonQuery();
                        }
                    }
                    catch (Exception e)
                    {
                        if (!script)
                        {
                            Console.WriteLine(dropSQL[i]);
                        }
                        Console.WriteLine("Unsuccessful: " + e.Message);
                    }
                }

                if (!justDrop)
                {
                    for (int j = 0; j < createSQL.Length; j++)
                    {
                        try
                        {
                            string formatted;
                            if (format)
                            {
                                formatted = Format(createSQL[j]);
                            }
                            else
                            {
                                formatted = createSQL[j];
                            }
                            if (delimiter != null)
                            {
                                formatted += delimiter;
                            }
                            if (script)
                            {
                                Console.WriteLine(formatted);
                            }
                            if (outputFile != null)
                            {
                                fileOutput.WriteLine(formatted);
                            }
                            if (export)
                            {
                                statement.CommandText = createSQL[j];
                                statement.CommandType = CommandType.Text;
                                statement.ExecuteNonQuery();
                            }
                        }
                        catch (Exception e)
                        {
                            if (!script)
                            {
                                Console.WriteLine(createSQL[j]);
                            }
                            Console.WriteLine("Unsuccessful: " + e.Message);

                            // Fail on create script errors
                            throw;
                        }
                    }
                }
            }
            catch (HibernateException)
            {
                // So that we don't wrap HibernateExceptions in HibernateExceptions
                throw;
            }
            catch (Exception e)
            {
                Console.Write(e.StackTrace);
                throw new HibernateException(e.Message, e);
            }
            finally
            {
                try
                {
                    if (statement != null)
                    {
                        statement.Dispose();
                    }
                    if (connection != null)
                    {
                        connectionProvider.CloseConnection(connection);
                        connectionProvider.Dispose();
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Could not close connection: " + e.Message);
                }
                if (fileOutput != null)
                {
                    try
                    {
                        fileOutput.Close();
                    }
                    catch (Exception ioe)
                    {
                        Console.Error.WriteLine("Error closing output file " + outputFile + ": " + ioe.Message);
                    }
                }
            }
        }
 public void Dispose()
 {
     _base.Dispose();
 }