Oracle分布式

来源:互联网 发布:淘宝默认几天确定收货 编辑:程序博客网 时间:2024/05/17 01:27

Oracle的分布式做的挺不错的,因为你只需要建立几个database link就可得到想要的东西。

如下展示:

首先在Oracle安装目录下找到一个叫做tnsnames.ora的文件。在其中添加一些内容例如:

LOANDB =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.3)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = LOANDB)
    )
  )

其中HOST = 192.168.1.3是你要连接的远程数据所在的ip地址。PORT = 1521是这个远程数据库的监听端口。SERVICE_NAME = LOANDB是远程的数据库名。

然后使用sqlplus登陆Oracle。

sqlplus

system@chicoDB as sysdba

然后输入你的密码

然后写这样一句话:

create public database link loan connect to system identified by feng using 'loanDB'

这里loan是你的连接名,以后就可以直接使用这个名字作为远端了。system是远程数据库的用户名。feng是那个用户名的密码。loanDB看上面的tnsnames.ora中的那一行LOANDB就是.

然后你要查询远程数据的中的system.client表就这样写一行:

select * from system.client@loan 就可以了。

如果你不小心把database link建错了,只需要drop public database link loan即可。

远程的更新稍稍有些问题。就是可能你看到不到数据库已经更新了。或者你这边看到更新了,但是远程的数据库好像并没有更新。这种问题的解决方案就是:在update或者delete语句之后使用commit;语句即可

Oracle事务处理用C#代码这样写:(我使用的ODBC)

        public Boolean ExecuteTransaction(string[] sqls)
        {
            const string connectionString = "DSN=fengDB;UID=System;Pwd=zaqwsx;";
            using (OdbcConnection connection =
                       new OdbcConnection(connectionString))

            {
                OdbcCommand command = new OdbcCommand();
                OdbcTransaction transaction = null;

                // Set the Connection to the new OdbcConnection.
                command.Connection = connection;

                // Open the connection and execute the transaction.
                try
                {
                    connection.Open();

                    // Start a local transaction
                    transaction = connection.BeginTransaction();

                    // Assign transaction object for a pending local transaction.
                    command.Connection = connection;
                    command.Transaction = transaction;
                    foreach (string sql in sqls)
                    {
                        System.Console.WriteLine(sql);
                        command.CommandText = sql;
                        command.ExecuteNonQuery();
                    }
                    // Execute the commands.

                    //command.CommandText =
                    //    "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
                    //command.ExecuteNonQuery();

                    // Commit the transaction.
                    transaction.Commit();
                    Console.WriteLine("Both records are written to database.");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    try
                    {
                        // Attempt to roll back the transaction.
                        transaction.Rollback();
                    }
                    catch
                    {
                        // Do nothing here; transaction is not active.
                    }
                    return false;
                }
                // The connection is automatically closed when the
                // code exits the using block.

            }
            return true;
        }
加红的地方好像必须这样写,不管你前面是否已经建立了连接。不过有一点不好的是,这样的事务处理只能批处理更新和删除...只能根据业务需求再重写这些。

 

 

原创粉丝点击