Exemplo n.º 1
0
        /// <summary>
        /// 获取Ado.Net Connection/Transaction Pair from the given connectionString
        /// </summary>
        /// <param name="connString">连接字符串</param>
        /// <returns>A Connection/Transaction Pair</returns>
        public ConnectionTxPair GetConnectionTxPair(string connString)
        {
            DbProvider.ConnectionString = connString;
            ConnectionTxPair connectionTxPair = ConnectionUtils.GetConnectionTxPair(DbProvider);

            return(connectionTxPair);
        }
Exemplo n.º 2
0
        protected virtual Dictionary <string, DataTable> BuildDatabase(IDictionary <string, Table> tables,
                                                                       SpringBaseDao baseDao, out bool createdDatabase)
        {
            createdDatabase = false;
            ConnectionTxPair connectionTxPairToUse = null;

            try
            {
                connectionTxPairToUse = ConnectionUtils.GetConnectionTxPair(baseDao.DbProvider);
                ConnectionUtils.DisposeConnection(connectionTxPairToUse.Connection, baseDao.DbProvider);
                connectionTxPairToUse = null;
                return(GetCurrentDatabaseTableSchemas(tables, baseDao));
            }
            catch (CannotGetAdoConnectionException)
            {
                // Database probably does not exist, so attempt to create it below
            }
            finally
            {
                if (connectionTxPairToUse != null)
                {
                    ConnectionUtils.DisposeConnection(connectionTxPairToUse.Connection, baseDao.DbProvider);
                    connectionTxPairToUse = null;
                }
            }
            string saveConnectionString = baseDao.DbProvider.ConnectionString;
            string databaseName;
            string newConnectionString =
                SpringBaseDao.RemoveDatabaseFromConnectionString(baseDao.DbProvider, out databaseName);

            if (string.IsNullOrEmpty(databaseName))
            {
                throw new ArgumentException(string.Format("Could not locate database name in connection string: \"{0}\"",
                                                          baseDao.DbProvider.ConnectionString));
            }
            baseDao.DbProvider.ConnectionString = newConnectionString;
            try
            {
                connectionTxPairToUse = ConnectionUtils.GetConnectionTxPair(baseDao.DbProvider);
                string sql = string.Format("CREATE DATABASE {0}", databaseName);
                baseDao.AdoTemplate.ExecuteNonQuery(CommandType.Text, sql);
                createdDatabase = true;
            }
            finally
            {
                if (connectionTxPairToUse != null)
                {
                    ConnectionUtils.DisposeConnection(connectionTxPairToUse.Connection, baseDao.DbProvider);
                    connectionTxPairToUse = null;
                }
                baseDao.DbProvider.ConnectionString = saveConnectionString;
            }
            // Wait for database to be fully created
            long timeoutTicks = DateTime.Now.Ticks + TimeSpan.FromSeconds(CREATE_DATABASE_WAIT_SECONDS).Ticks;

            do
            {
                try
                {
                    return(GetCurrentDatabaseTableSchemas(tables, baseDao));
                }
                catch (DataAccessException)
                {
                }
                Thread.Sleep(300);
            }while (timeoutTicks > DateTime.Now.Ticks);
            throw new CannotGetAdoConnectionException();
        }