| 
 | 1 | +/* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. */  | 
 | 2 | +   | 
 | 3 | +/******************************************************************************  | 
 | 4 | + *  | 
 | 5 | + * You may not use the identified files except in compliance with The MIT  | 
 | 6 | + * License (the "License.")  | 
 | 7 | + *  | 
 | 8 | + * You may obtain a copy of the License at  | 
 | 9 | + * https://github.com/oracle/Oracle.NET/blob/master/LICENSE  | 
 | 10 | + *  | 
 | 11 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT  | 
 | 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 14 | + *  | 
 | 15 | + * See the License for the specific language governing permissions and  | 
 | 16 | + * limitations under the License.  | 
 | 17 | + *  | 
 | 18 | + *****************************************************************************/  | 
 | 19 | +   | 
 | 20 | +using System;  | 
 | 21 | +using System.Threading;  | 
 | 22 | +using System.Transactions;  | 
 | 23 | +using Oracle.DataAccess.Client;  | 
 | 24 | + | 
 | 25 | +namespace AppContinuity  | 
 | 26 | +{  | 
 | 27 | +    class Program  | 
 | 28 | +    {  | 
 | 29 | +        static void Main(string[] args)  | 
 | 30 | +        {  | 
 | 31 | + | 
 | 32 | +            //Demo: ODP.NET Application Continuity  | 
 | 33 | +            //Demonstrates that applications can recover and continue after   | 
 | 34 | +            // recoverable errors without end user disruption. Recoverable errors  | 
 | 35 | +            // are errors that arise due to an external system failure,   | 
 | 36 | +            // independent of the application session logic that is executing.   | 
 | 37 | +            // Recoverable errors occur following planned and unplanned outages   | 
 | 38 | +            // of foregrounds, networks, nodes, storage, and databases, such as a   | 
 | 39 | +            // network outage, instance failure, hardware failure, network failure,   | 
 | 40 | +            // configuration change, or patching.  | 
 | 41 | + | 
 | 42 | +            //To use Application Continuity with ODP.NET, set   | 
 | 43 | +            // Application Continuity=true (default) in the connection string.  | 
 | 44 | + | 
 | 45 | +            //Setup instructions:  | 
 | 46 | +            // A) On the database server :  | 
 | 47 | +            //  1) Setup HR schema by using script %ORACLE_HOME%\demo\schema\human_resources\hr_main.sql   | 
 | 48 | +            //      if not already available.  | 
 | 49 | +            //  2) Run "GRANT execute on DBMS_APP_CONT to HR;" so that ODP.NET can determine the   | 
 | 50 | +            //      in-flight transaction status following a recoverable error.  | 
 | 51 | +            // B) Modify the following connection attributes in this sample code:  | 
 | 52 | +            //  1) Password: Password you specified while setting up HR schema setup.  | 
 | 53 | +            //  2) Data Source: Connection descriptor or TNS alias to connect to the database.  | 
 | 54 | + | 
 | 55 | +            //Runtime instructions:  | 
 | 56 | +            // You can intermittently execute the following command on the database  | 
 | 57 | +            // to simulate a recoverable error condition and observe AC in action:  | 
 | 58 | +            // "Execute dbms_service.disconnect_session('your service', dbms_service.immediate );"  | 
 | 59 | +            //  | 
 | 60 | +            // With Application Continuity enabled, the end user will observe   | 
 | 61 | +            // no error message, only a slight delayed execution.  | 
 | 62 | + | 
 | 63 | +            //Provide user id and password for Oracle user	  | 
 | 64 | +            string conString = "User Id=hr;Password=<password>;" +  | 
 | 65 | + | 
 | 66 | +            //Provide Oracle data source info.  | 
 | 67 | +            "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=<hostname or IP>)(PORT=<port>))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=<service name>)));";  | 
 | 68 | + | 
 | 69 | +            //Loop infinitely  | 
 | 70 | +            while (true)  | 
 | 71 | +            {  | 
 | 72 | +                try  | 
 | 73 | +                {  | 
 | 74 | +                    using (OracleConnection con = new OracleConnection(conString))  | 
 | 75 | +                    {  | 
 | 76 | +                        using (OracleCommand cmd = con.CreateCommand())  | 
 | 77 | +                        {  | 
 | 78 | +                            //Start transaction  | 
 | 79 | +                            using (TransactionScope scope = new TransactionScope())  | 
 | 80 | +                            {  | 
 | 81 | +                                con.Open();  | 
 | 82 | +                                cmd.CommandText = "update employees set salary=salary+1 where employee_id = 100";  | 
 | 83 | + | 
 | 84 | +                                cmd.ExecuteNonQuery();  | 
 | 85 | + | 
 | 86 | +                                //Commit  | 
 | 87 | +                                scope.Complete();  | 
 | 88 | + | 
 | 89 | +                                Console.WriteLine("Salary incremented.");  | 
 | 90 | +                            }  | 
 | 91 | +                            //Sleep for 1 second  | 
 | 92 | +                            Thread.Sleep(1000);  | 
 | 93 | +                        }  | 
 | 94 | +                    }  | 
 | 95 | +                }  | 
 | 96 | +                catch (Exception ex)  | 
 | 97 | +                {  | 
 | 98 | +                    Console.WriteLine(ex.Message);  | 
 | 99 | +                }  | 
 | 100 | +            }  | 
 | 101 | +        }  | 
 | 102 | +    }  | 
 | 103 | +}  | 
0 commit comments