DBA Data[Home] [Help]

PACKAGE BODY: APPS.FND_ADG_UTILITY

Source


1 package body fnd_adg_utility as
2 /* $Header: AFDGUTLB.pls 120.8 2010/09/08 09:23:31 rsanders noship $ */
3 
4 C_CONTROL_KEY		constant	number 	:= 1;
5 
6 C_FORCE_PUBLIC_DBLINK   constant        boolean      := true;
7 
8 C_MAX_STANDBY_SYSTEMS   constant        number  := 5;
9 
10 G_SESS_SIMULATED_STDBY_ENABLED boolean := null;
11 G_SESS_COMMIT_WAIT_ENABLED  boolean := null;
12 G_MAGIC_SWITCH_ENABLED      boolean  := false;
13 
14 G_ENABLE_CONTROL_CACHE	    boolean  := false;
15 G_CONTROL_CACHE_LOADED	    boolean  := false;
16 G_CACHED_CONTROL_REC	    fnd_adg_control%rowtype;
17 
18 	/* RPC state */
19 
20 C_RPC_SYSTEM_ENABLED    constant        number  := 1;
21 C_RPC_SIMULATION_VALIDATED constant	number  := 2;
22 C_RPC_ADG_VALIDATED	constant	number	:= 4;
23 C_RPC_ADG_ENABLED       constant        number  := 8;
24 C_RPC_SYSTEM_PREPARED   constant        number  := 16;
25 C_RPC_RUNT_VALIDATE_TIMESTAMP
26                         constant	number	:= 32;
27 
28 C_OPEN_READ_ONLY        constant        varchar2(30) := 'READ ONLY';
29 C_OPEN_READ_WRITE       constant        varchar2(30) := 'READ WRITE';
30 C_STANDBY_ROLE          constant        varchar2(30) := 'PHYSICAL STANDBY';
31 C_PRIMARY_ROLE          constant        varchar2(30) := 'PRIMARY';
32 
33 C_MAGIC_SWITCH_EVENT_ON constant	varchar2(255) :=
34                             '3177 trace name context forever, level 1';
35 C_MAGIC_SWITCH_EVENT_OFF constant	varchar2(255) :=
36                             '3177 trace name context forever, level 0';
37 C_MAGIC_SWITCH_IDENT    constant 	varchar2(255) :=
38 			    '*** READ-ONLY VIOLATION BY MODULE ';
39 			    -- '*ADG-ACCESS-VIOLATION-INFO: *';
40 
41 	/* Thresholds */
42 
43 C_STD_ERROR_THRESHOLD	constant	number	     := 5;
44 C_MIN_ERROR_THRESHOLD	constant	number	     := 0;
45 C_MAX_ERROR_THRESHOLD	constant	number	     := 25;
46 
47 	/* CHECK MODE */
48 
49 	/* Access codes */
50 
51 G_CONC_PROGRAM_ACCESS_CODE	number;
52 
53 	/* Database triggers */
54 
55 C_ERROR_TRIGGER		constant	varchar2(30) := 'FND_ADG_ERROR_TRIGGER';
56 C_LOGON_TRIGGER		constant        varchar2(30) := 'FND_ADG_LOGON_TRIGGER';
57 C_LOGOFF_TRIGGER	constant        varchar2(30) := 'FND_ADG_LOGOFF_TRIGGER';
58 
59 	/* Max length of CONNSTR. */
60 
61 C_MAX_CONNSTR_LENGTH	constant        number       := 128;
62 
63 /*==========================================================================*/
64 
65 procedure check_standby_support
66 as
67 begin
68 
69   if ( not is_standby_access_supported )
70   then
71      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_INVALID_DB_RELEASE);
72   end if;
73 
74 end;
75 
76 /*==========================================================================*/
77 
78 procedure check_connection_type(p_type number)
79 as
80 begin
81 
82   case p_type
83     when C_CONNECT_STANDBY_TO_PRIMARY   then return;
84     when C_CONNECT_PRIMARY_TO_STANDBY   then return;
85     when C_CONNECT_TO_SIMULATED_STANDBY then return;
86     else
87        fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_INVALID_CONNECT_TYPE);
88   end case;
89 
90 end;
91 
92 /*==========================================================================*/
93 
94 procedure check_standby_number(p_standby_number number)
95 as
96 begin
97 
98   if ( p_standby_number is null )
99   then
100      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_STANDBY_NULL);
101   end if;
102 
103   if ( p_standby_number < 1 or p_standby_number > C_MAX_STANDBY_SYSTEMS )
104   then
105      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_STANDBY_OUT_OF_RANGE);
106   end if;
107 
108 end;
109 
110 /*==========================================================================*/
111 
112 function is_rpc_state(p_rec fnd_adg_control%rowtype,
113                       p_flag number
114                      ) return boolean
115 as
116 begin
117 
118   if ( bitand(p_rec.rpc_system_state,p_flag) <> 0 )
119   then
120      return true;
121   else
122      return false;
123   end if;
124 
125 end;
126 
127 /*==========================================================================*/
128 
129 procedure set_rpc_state(p_rec in out nocopy fnd_adg_control%rowtype,
130                         p_flag number)
131 as
132 begin
133 
134   if ( not is_rpc_state(p_rec,p_flag) )
135   then
136      p_rec.rpc_system_state := p_rec.rpc_system_state + p_flag;
137   end if;
138 
139 end;
140 
141 /*==========================================================================*/
142 
143 procedure clr_rpc_state(p_rec in out nocopy fnd_adg_control%rowtype,
144                         p_flag number)
145 as
146 begin
147 
148   if ( is_rpc_state(p_rec,p_flag) )
149   then
150      p_rec.rpc_system_state := p_rec.rpc_system_state - p_flag;
151   end if;
152 
153 end;
154 
155 /*==========================================================================*/
156 
157 function boolean_to_yn(p_bool boolean) return varchar2
158 as
159 begin
160 
161   if ( p_bool is null )
162   then
163      return null;
164   end if;
165 
166   if ( p_bool )
167   then
168      return 'Y';
169   else
170      return 'N';
171   end if;
172 
173 end;
174 
175 /*==========================================================================*/
176 
177 function yn_to_boolean(p_yn varchar2) return boolean
178 as
179 begin
180 
181   if ( p_yn is null )
182   then
183      return null;
184   end if;
185 
186   if ( upper(p_yn) = 'Y' )
187   then
188      return true;
189   else
190      return false;
191   end if;
192 
193 end;
194 
195 /*==========================================================================*/
196 
197 procedure set_program_access_code
198 as
199 begin
200 
201   G_CONC_PROGRAM_ACCESS_CODE := dbms_random.value;
202 
203 end;
204 
205 /*==========================================================================*/
206 
207 procedure init_adg_control(p_rec in out nocopy fnd_adg_control%rowtype)
208 as
209 begin
210 
211   p_rec.control_key                  := C_CONTROL_KEY;
212   p_rec.enable_adg_support           := 'N';
213   p_rec.rpc_system_state             := 0;
214   p_rec.enable_commit_wait           := 'Y';
215   p_rec.max_commit_wait_time         := 60;
216   p_rec.runtime_validate_timestamp   := 'Y';
217   p_rec.always_collect_primary_data  := 'Y';
218   p_rec.enable_redirect_if_valid     := 'N';
219   p_rec.standby_error_threshold      := C_STD_ERROR_THRESHOLD;
220   p_rec.simulation_error_threshold   := C_STD_ERROR_THRESHOLD;
221   p_rec.enable_standby_error_checks  := 'Y';
222   p_rec.enable_automatic_redirection := 'N';
223   p_rec.stndby_to_primary_link_owner := null;
224   p_rec.stndby_to_primary_link_name  := null;
225   p_rec.stndby_to_primary_link_valid := 'N';
226   p_rec.stndby_to_primary_connstr    := null;
227   p_rec.enable_simulated_standby     := 'Y';
228   p_rec.enable_auto_simulated_standby:= 'N';
229   p_rec.simulated_standby_service    := null;
230   p_rec.simulated_stndby_link_owner  := null;
231   p_rec.simulated_stndby_link_name   := null;
232   p_rec.simulated_stndby_link_valid  := 'N';
233   p_rec.simulated_stndby_connstr     := null;
234   p_rec.simulated_stndby_trc_dir_obj := null;
235   p_rec.primary_to_stndby1_link_owner:= null;
236   p_rec.primary_to_stndby1_link_name := null;
237   p_rec.primary_to_stndby1_link_valid:= 'N';
238   p_rec.primary_to_stndby1_connstr   := null;
239   p_rec.mgr_stndby1_req_class_app_id := null;
240   p_rec.mgr_stndby1_req_class_id     := null;
241   p_rec.primary_to_stndby2_link_owner:= null;
242   p_rec.primary_to_stndby2_link_name := null;
243   p_rec.primary_to_stndby2_link_valid:= 'N';
244   p_rec.primary_to_stndby2_connstr   := null;
245   p_rec.mgr_stndby2_req_class_app_id := null;
246   p_rec.mgr_stndby2_req_class_id     := null;
247   p_rec.primary_to_stndby3_link_owner:= null;
248   p_rec.primary_to_stndby3_link_name := null;
249   p_rec.primary_to_stndby3_link_valid:= 'N';
250   p_rec.primary_to_stndby3_connstr   := null;
251   p_rec.mgr_stndby3_req_class_app_id := null;
252   p_rec.mgr_stndby3_req_class_id     := null;
253   p_rec.primary_to_stndby4_link_owner:= null;
254   p_rec.primary_to_stndby4_link_name := null;
255   p_rec.primary_to_stndby4_link_valid:= 'N';
256   p_rec.primary_to_stndby4_connstr   := null;
257   p_rec.mgr_stndby4_req_class_app_id := null;
258   p_rec.mgr_stndby4_req_class_id     := null;
259   p_rec.primary_to_stndby5_link_owner:= null;
260   p_rec.primary_to_stndby5_link_name := null;
261   p_rec.primary_to_stndby5_link_valid:= 'N';
262   p_rec.primary_to_stndby5_connstr   := null;
263   p_rec.mgr_stndby5_req_class_app_id := null;
264   p_rec.mgr_stndby5_req_class_id     := null;
265   p_rec.debug_slave_rpc              := 0;
266   p_rec.debug_rpc                    := 0;
267 
268 end;
269 
270 /*==========================================================================*/
271 
272 	-- auto_init_adg
273 
274 	-- This is a one time boot function to auto create data.
275 
276 procedure auto_init_adg
277 as
278 PRAGMA AUTONOMOUS_TRANSACTION;
279 l_rec fnd_adg_control%rowtype;
280 begin
281 
282   init_adg_control(l_rec);
283 
284   begin
285 
286     insert into fnd_adg_control values l_rec;
287 
288   exception
289     when DUP_VAL_ON_INDEX then
290          commit;
291          return;	-- someone got here first.
292   end;
293 
294 	-- If we're here then we've locked the control rec.
295 
296   fnd_adg_object.init_package_list;
297 
298   commit;
299 
300 end;
301 
302 /*==========================================================================*/
303 
304 function get_adg_control return fnd_adg_control%rowtype
305 as
306 l_adg_control_rec fnd_adg_control%rowtype;
307 begin
308 
309   if ( G_ENABLE_CONTROL_CACHE and G_CONTROL_CACHE_LOADED )
310   then
311      l_adg_control_rec := G_CACHED_CONTROL_REC;
312      return l_adg_control_rec;
313   end if;
314 
315   begin
316 
317     select a.*
318       into l_adg_control_rec
319       from fnd_adg_control a
320      where a.control_key = C_CONTROL_KEY;
321 
322   exception
323      when no_data_found then
324 
325         auto_init_adg;
326 
327         select a.*
328           into l_adg_control_rec
329           from fnd_adg_control a
330          where a.control_key = C_CONTROL_KEY;
331 
332   end;
333 
334   if ( G_ENABLE_CONTROL_CACHE )
335   then
336      G_CACHED_CONTROL_REC := l_adg_control_rec;
337      G_CONTROL_CACHE_LOADED := true;
338   end if;
339 
340   return l_adg_control_rec;
341 
342 end;
343 
344 /*==========================================================================*/
345 
346 function get_and_lock_adg_control return fnd_adg_control%rowtype
347 as
348 l_adg_control_rec fnd_adg_control%rowtype;
349 begin
350 
351 	-- Updates always disable cache
352 
353   disable_control_cache;
354 
355   select a.*
356     into l_adg_control_rec
357     from fnd_adg_control a
358    where a.control_key = C_CONTROL_KEY
359      for update of a.control_key;
360 
361   return l_adg_control_rec;
362 
363 end;
364 
365 /*==========================================================================*/
366 
367 function compile_directive_state return boolean
368 as
369 l_compile_state number := -1 ;
370 begin
371 
372   execute immediate
373           ' declare l_rc number := 0 ; ' ||
374           ' begin ' ||
375           '   if ( fnd_adg_compile_directive.enable_rpc ) ' ||
376           '   then ' ||
377           '       l_rc := 1; ' ||
378           '   end if; ' ||
379           '   :1 := l_rc; ' ||
380           ' end; '
381           using in out l_compile_state;
382 
383   if ( l_compile_state = 1)
384   then
385      return true;
386   else
387      return false;
388   end if;
389 
390 end;
391 
392 /*==========================================================================*/
393 
394 procedure check_rpc_state(p_state_on boolean)
395 as
396 l_rec fnd_adg_control%rowtype;
397 l_compile_state boolean;
398 
399 begin
400 
401   l_rec := get_adg_control;
402 
403   if ( p_state_on )
404   then
405      if ( not is_rpc_state(l_rec,C_RPC_SYSTEM_ENABLED) )
406      then
407         fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_RPC_SYSTEM_OFF);
408      end if;
409   else
410      if ( is_rpc_state(l_rec,C_RPC_SYSTEM_ENABLED) )
411      then
412         fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_RPC_SYSTEM_ON);
413      end if;
414   end if;
415 
416 	/* Consistency check */
417 
418   l_compile_state := compile_directive_state;
419 
420   if ( ( l_compile_state and p_state_on ) or
421        ( not l_compile_state and not p_state_on ) )
422   then
423      null;
424   else
425      fnd_adg_exception.raise_error
426                     (fnd_adg_exception.C_UTLERR_DIRECTIVE_MISMATCH);
427   end if;
428 
429 end;
430 
431 /*==========================================================================*/
432 
433 procedure check_adg_state(p_state_on boolean)
434 as
435 l_rec fnd_adg_control%rowtype;
436 begin
437 
438   l_rec := get_adg_control;
439 
440   if ( p_state_on )
441   then
442      if ( not is_rpc_state(l_rec,C_RPC_ADG_ENABLED   ) )
443      then
444         fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_RPC_ADG_OFF);
445      end if;
446   else
447      if ( is_rpc_state(l_rec,C_RPC_ADG_ENABLED   ) )
448      then
449         fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_RPC_ADG_ON);
450      end if;
451   end if;
452 
453 	/* Consistency check */
454 
455   if ( ( l_rec.enable_adg_support = 'Y' and p_state_on ) or
456        ( l_rec.enable_adg_support = 'N' and not p_state_on ) )
457   then
458      null;
459   else
460      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_INCONSISTENT_ADGSTATE);
461   end if;
462 
463 end;
464 
465 /*==========================================================================*/
466 
467 procedure set_session_simulated_stdby
468 as
469 l_rec fnd_adg_control%rowtype;
470 
471 cursor c1 is select a.service_name
472                from v$session a
473               where a.sid = ( select distinct b.sid from v$mystat b);
474 
475 begin
476 
477   G_SESS_SIMULATED_STDBY_ENABLED := false;
478 
479   if ( not is_standby_access_supported )
480   then
481      return;
482   end if;
483 
484   if ( not is_adg_support_enabled )
485   then
486      return;
487   end if;
488 
489   l_rec := get_adg_control;
490 
491   if ( is_simulated_standby_enabled )
492   then
493      for f_sess in c1 loop
494 
495        if ( upper(f_sess.service_name) = l_rec.simulated_standby_service )
496        then
497           G_SESS_SIMULATED_STDBY_ENABLED := true;
498        end if;
499 
500        exit;
501      end loop;
502 
503   end if;
504 
505 end;
506 
507 /*==========================================================================*/
508 
509 procedure set_commit_wait_enabled
510 as
511 l_rec fnd_adg_control%rowtype;
512 begin
513 
514   G_SESS_COMMIT_WAIT_ENABLED := false;
515 
516   l_rec := get_adg_control;
517 
518   if ( yn_to_boolean(l_rec.enable_commit_wait) )
519   then
520      G_SESS_COMMIT_WAIT_ENABLED := true;
521   end if;
522 
523 end;
524 
525 /*==========================================================================*/
526 procedure log_adg_violations(p_request_id number,
527                              p_adg_violations number,
528                              p_magic_switch_enabled varchar2,
529                              p_trace_file varchar2,
530                              p_trace_error number)
531 as
532 PRAGMA AUTONOMOUS_TRANSACTION;
533 cursor c1 is select b.pid,b.spid,a.audsid,a.process
534                from v$session a,v$process b
535               where a.sid = ( select distinct c.sid from v$mystat c)
536                 and a.paddr = b.addr;
537 
538 begin
539 
540   for f_rec in c1 loop
541 
542     insert into fnd_adg_simulated_stndby_trc
543         (
544           log_seq               ,
545           ORACLE_PROCESS_ID     ,
546           OS_PROCESS_ID         ,
547           ORACLE_SESSION_ID     ,
548           REQUEST_ID            ,
549           magic_switch_enabled  ,
550           read_only_violations  ,
551           trace_file_name      ,
552           trace_access_error
553         )
554       values
555         (
556           fnd_adg_simulated_stndby_trc_s.nextval,
557           f_rec.spid,
558           substr(f_rec.process,1,50),
559           f_rec.audsid,
560           p_request_id,
561           p_magic_switch_enabled,
562           p_adg_violations          ,
563           p_trace_file,
564           p_trace_error
565         );
566 
567     exit;
568 
569   end loop;
570 
571   commit;
572 
573 end;
574 
575 /*==========================================================================*/
576 
577 procedure set_adg_control(p_rec in out nocopy fnd_adg_control%rowtype)
578 as
579 begin
580 
581   p_rec.enable_adg_support           := 'N';
582   p_rec.enable_commit_wait           := 'Y';
583   p_rec.max_commit_wait_time         := 60;
584   p_rec.runtime_validate_timestamp   := 'Y';
585   p_rec.always_collect_primary_data  := 'Y';
586   p_rec.enable_redirect_if_valid     := 'N';
587   p_rec.standby_error_threshold      := C_STD_ERROR_THRESHOLD;
588   p_rec.simulation_error_threshold   := C_STD_ERROR_THRESHOLD;
589   p_rec.enable_standby_error_checks  := 'Y';
590   p_rec.enable_automatic_redirection := 'N';
591   p_rec.stndby_to_primary_link_owner := null;
592   p_rec.stndby_to_primary_link_name  := null;
593   p_rec.stndby_to_primary_link_valid := 'N';
594   p_rec.stndby_to_primary_connstr    := null;
595   p_rec.enable_simulated_standby     := 'Y';
596   p_rec.enable_auto_simulated_standby:= 'N';
597   p_rec.simulated_standby_service    := null;
598   p_rec.simulated_stndby_link_owner  := null;
599   p_rec.simulated_stndby_link_name   := null;
600   p_rec.simulated_stndby_link_valid  := 'N';
601   p_rec.simulated_stndby_connstr     := null;
602   p_rec.simulated_stndby_trc_dir_obj := null;
603   p_rec.primary_to_stndby1_link_owner:= null;
604   p_rec.primary_to_stndby1_link_name := null;
605   p_rec.primary_to_stndby1_link_valid:= 'N';
606   p_rec.primary_to_stndby1_connstr   := null;
607   p_rec.mgr_stndby1_req_class_app_id := null;
608   p_rec.mgr_stndby1_req_class_id     := null;
609   p_rec.primary_to_stndby2_link_owner:= null;
610   p_rec.primary_to_stndby2_link_name := null;
611   p_rec.primary_to_stndby2_link_valid:= 'N';
612   p_rec.primary_to_stndby2_connstr   := null;
613   p_rec.mgr_stndby2_req_class_app_id := null;
614   p_rec.mgr_stndby2_req_class_id     := null;
615   p_rec.primary_to_stndby3_link_owner:= null;
616   p_rec.primary_to_stndby3_link_name := null;
617   p_rec.primary_to_stndby3_link_valid:= 'N';
618   p_rec.primary_to_stndby3_connstr   := null;
619   p_rec.mgr_stndby3_req_class_app_id := null;
620   p_rec.mgr_stndby3_req_class_id     := null;
621   p_rec.primary_to_stndby4_link_owner:= null;
622   p_rec.primary_to_stndby4_link_name := null;
623   p_rec.primary_to_stndby4_link_valid:= 'N';
624   p_rec.primary_to_stndby4_connstr   := null;
625   p_rec.mgr_stndby4_req_class_app_id := null;
626   p_rec.mgr_stndby4_req_class_id     := null;
627   p_rec.primary_to_stndby5_link_owner:= null;
628   p_rec.primary_to_stndby5_link_name := null;
629   p_rec.primary_to_stndby5_link_valid:= 'N';
630   p_rec.primary_to_stndby5_connstr   := null;
631   p_rec.mgr_stndby5_req_class_app_id := null;
632   p_rec.mgr_stndby5_req_class_id     := null;
633   p_rec.debug_slave_rpc              := 0;
634   p_rec.debug_rpc                    := 0;
635 
636 end;
637 
638 /*==========================================================================*/
639 
640 procedure create_adg_control(p_commit boolean default true,
641                              p_ignore_row_exists boolean default false)
642 as
643 l_rec fnd_adg_control%rowtype;
644 begin
645 
646   init_adg_control(l_rec);
647 
648   insert into fnd_adg_control values l_rec;
649 
650   if ( p_commit )
651   then
652      commit;
653   end if;
654 
655 exception
656   when DUP_VAL_ON_INDEX then
657 
658        if ( p_ignore_row_exists )
659        then
660           null;
661        else
662           raise;
663        end if;
664 
665 end;
666 
667 /*==========================================================================*/
668 
669 procedure update_adg_control(p_adg_control_rec fnd_adg_control%rowtype,
670                              p_commit boolean default true)
671 as
672 begin
673 
674   update fnd_adg_control a
675      set row = p_adg_control_rec
676    where a.control_key = C_CONTROL_KEY;
677 
678   if ( p_commit )
679   then
680      commit;
681   end if;
682 
683 end;
684 
685 /*==========================================================================*/
686 
687 procedure clean_adg_control(p_commit boolean default true,
688                             p_clean_all boolean default false,
689                             p_create_on_demand boolean default false)
690 as
691 l_rec fnd_adg_control%rowtype;
692 begin
693 
694 	-- Dummy get_adg_control. This ensure that on a fresh system
695 	-- we auto_init the required data.
696 
697   l_rec := get_adg_control;
698 
699   if ( p_create_on_demand )
700   then
701      create_adg_control(false,true);
702   end if;
703 
704   l_rec := get_and_lock_adg_control;
705 
706   if ( p_clean_all )
707   then
708      init_adg_control(l_rec);
709   else
710      set_adg_control(l_rec);
711   end if;
712 
713   update_adg_control(l_rec,false);
714 
715   if ( p_commit )
716   then
717      commit;
718   end if;
719 
720 end;
721 
722 /*==========================================================================*/
723 
724 function find_primary_to_standby(p_rec fnd_adg_control%rowtype,
725                                  p_standby_number number,
726                                  p_connstr varchar2) return boolean
727 as
728 begin
729 
730   case p_standby_number
731 
732     when 1 then
733                 if ( p_rec.primary_to_stndby1_connstr is not null and
734                      p_rec.primary_to_stndby1_connstr = p_connstr )
735                 then
736                    return true;
737                 end if;
738     when 2 then
739                 if ( p_rec.primary_to_stndby2_connstr is not null and
740                      p_rec.primary_to_stndby2_connstr = p_connstr )
741                 then
742                    return true;
743                 end if;
744     when 3 then
745                 if ( p_rec.primary_to_stndby3_connstr is not null and
746                      p_rec.primary_to_stndby3_connstr = p_connstr )
747                 then
748                    return true;
749                 end if;
750     when 4 then
751                 if ( p_rec.primary_to_stndby4_connstr is not null and
752                      p_rec.primary_to_stndby4_connstr = p_connstr )
753                 then
754                    return true;
755                 end if;
756     when 5 then
757                 if ( p_rec.primary_to_stndby5_connstr is not null and
758                      p_rec.primary_to_stndby5_connstr = p_connstr )
759                 then
760                    return true;
761                 end if;
762   end case;
763 
764   return false;
765 
766 end;
767 
768 /*==========================================================================*/
769 
770 procedure set_cm_class_data(p_rec in out nocopy fnd_adg_control%rowtype,
771                             p_standby_number number,
772                             p_req_class_app_id number,
773                             p_req_class_id number )
774 as
775 begin
776 
777   case p_standby_number
778 
779       when 1 then
780                   p_rec.mgr_stndby1_req_class_app_id := p_req_class_app_id;
781                   p_rec.mgr_stndby1_req_class_id     := p_req_class_id;
782       when 2 then
783                   p_rec.mgr_stndby2_req_class_app_id := p_req_class_app_id;
784                   p_rec.mgr_stndby2_req_class_id     := p_req_class_id;
785       when 3 then
786                   p_rec.mgr_stndby3_req_class_app_id := p_req_class_app_id;
787                   p_rec.mgr_stndby3_req_class_id     := p_req_class_id;
788       when 4 then
789                   p_rec.mgr_stndby4_req_class_app_id := p_req_class_app_id;
790                   p_rec.mgr_stndby4_req_class_id     := p_req_class_id;
791       when 5 then
792                   p_rec.mgr_stndby5_req_class_app_id := p_req_class_app_id;
793                   p_rec.mgr_stndby5_req_class_id     := p_req_class_id;
794 
795     end case;
796 
797 end;
798 
799 /*==========================================================================*/
800 
801 procedure get_cm_class_data(p_rec in fnd_adg_control%rowtype,
802                             p_standby_number number,
803                             p_req_class_app_id in out nocopy number,
804                             p_req_class_id in out nocopy number )
805 as
806 begin
807 
808   case p_standby_number
809 
810       when 1 then
811                   p_req_class_app_id := p_rec.mgr_stndby1_req_class_app_id ;
812                   p_req_class_id     := p_rec.mgr_stndby1_req_class_id     ;
813       when 2 then
814                   p_req_class_app_id := p_rec.mgr_stndby2_req_class_app_id ;
815                   p_req_class_id     := p_rec.mgr_stndby2_req_class_id     ;
816       when 3 then
817                   p_req_class_app_id := p_rec.mgr_stndby3_req_class_app_id ;
818                   p_req_class_id     := p_rec.mgr_stndby3_req_class_id     ;
819       when 4 then
820                   p_req_class_app_id := p_rec.mgr_stndby4_req_class_app_id ;
821                   p_req_class_id     := p_rec.mgr_stndby4_req_class_id     ;
822       when 5 then
823                   p_req_class_app_id := p_rec.mgr_stndby5_req_class_app_id ;
824                   p_req_class_id     := p_rec.mgr_stndby5_req_class_id     ;
825 
826   end case;
827 
828 end;
829 
830 /*==========================================================================*/
831 
832 procedure get_connection_data(p_type number,
833                               p_rec in fnd_adg_control%rowtype,
834                               p_standby_number number,
835                               p_link_name in out nocopy varchar2,
836                               p_link_owner in out nocopy varchar2,
837                               p_link_connstr in out nocopy varchar2,
838                               p_link_valid in out nocopy varchar2,
839                               p_link_service in out nocopy varchar2
840                              )
841 as
842 begin
843 
844   case p_type
845 
846     when C_CONNECT_STANDBY_TO_PRIMARY then
847 
848          p_link_owner   := p_rec.stndby_to_primary_link_owner;
849          p_link_name    := p_rec.stndby_to_primary_link_name;
850          p_link_valid   := p_rec.stndby_to_primary_link_valid;
851          p_link_connstr := p_rec.stndby_to_primary_connstr;
852          p_link_service := null;
853 
854     when C_CONNECT_PRIMARY_TO_STANDBY then
855 
856          p_link_service := null;
857 
858          case p_standby_number
859 
860            when 1 then
861                        p_link_owner   := p_rec.primary_to_stndby1_link_owner;
862                        p_link_name    := p_rec.primary_to_stndby1_link_name ;
863                        p_link_valid   := p_rec.primary_to_stndby1_link_valid;
864                        p_link_connstr := p_rec.primary_to_stndby1_connstr   ;
865            when 2 then
866                        p_link_owner   := p_rec.primary_to_stndby2_link_owner;
867                        p_link_name    := p_rec.primary_to_stndby2_link_name ;
868                        p_link_valid   := p_rec.primary_to_stndby2_link_valid;
869                        p_link_connstr := p_rec.primary_to_stndby2_connstr   ;
870            when 3 then
871                        p_link_owner   := p_rec.primary_to_stndby3_link_owner;
872                        p_link_name    := p_rec.primary_to_stndby3_link_name ;
873                        p_link_valid   := p_rec.primary_to_stndby3_link_valid;
874                        p_link_connstr := p_rec.primary_to_stndby3_connstr   ;
875            when 4 then
876                        p_link_owner   := p_rec.primary_to_stndby4_link_owner;
877                        p_link_name    := p_rec.primary_to_stndby4_link_name ;
878                        p_link_valid   := p_rec.primary_to_stndby4_link_valid;
879                        p_link_connstr := p_rec.primary_to_stndby4_connstr   ;
880            when 5 then
881                        p_link_owner   := p_rec.primary_to_stndby5_link_owner;
882                        p_link_name    := p_rec.primary_to_stndby5_link_name ;
883                        p_link_valid   := p_rec.primary_to_stndby5_link_valid;
884                        p_link_connstr := p_rec.primary_to_stndby5_connstr   ;
885 
886          end case;
887 
888     when C_CONNECT_TO_SIMULATED_STANDBY then
889 
890          p_link_owner   := p_rec.simulated_stndby_link_owner;
891          p_link_name    := p_rec.simulated_stndby_link_name;
892          p_link_valid   := p_rec.simulated_stndby_link_valid;
893          p_link_connstr := p_rec.simulated_stndby_connstr;
894          p_link_service := p_rec.simulated_standby_service;
895 
896   end case;
897 
898 end;
899 
900 /*==========================================================================*/
901 
902 procedure set_connection_valid(p_type number,
903                                p_rec in out nocopy fnd_adg_control%rowtype,
904                                p_status boolean,
905                                p_standby_number number)
906 as
907 begin
908 
909   case p_type
910 
911     when C_CONNECT_STANDBY_TO_PRIMARY then
912 
913          p_rec.stndby_to_primary_link_valid := boolean_to_yn(p_status);
914 
915     when C_CONNECT_PRIMARY_TO_STANDBY then
916 
917          case p_standby_number
918 
919            when 1 then
920                   p_rec.primary_to_stndby1_link_valid:= boolean_to_yn(p_status);
921            when 2 then
922                   p_rec.primary_to_stndby2_link_valid:= boolean_to_yn(p_status);
923            when 3 then
924                   p_rec.primary_to_stndby3_link_valid:= boolean_to_yn(p_status);
925            when 4 then
926                   p_rec.primary_to_stndby4_link_valid:= boolean_to_yn(p_status);
927            when 5 then
928                   p_rec.primary_to_stndby5_link_valid:= boolean_to_yn(p_status);
929          end case;
930 
931     when C_CONNECT_TO_SIMULATED_STANDBY then
932 
933          p_rec.simulated_stndby_link_valid := boolean_to_yn(p_status);
934 
935   end case;
936 
937 end;
938 
939 /*==========================================================================*/
940 
941 function get_connection_type_info(p_type number,
942                                   p_standby_number number default null)
943                                      return varchar2
944 as
945 l_standby_no_info varchar2(100);
946 begin
947 
948   if ( p_standby_number is null )
949   then
950      l_standby_no_info := '';
951   else
952      l_standby_no_info := ' Number ' || p_standby_number;
953   end if;
954 
955   case p_type
956 
957     when C_CONNECT_STANDBY_TO_PRIMARY   then
958 
959          return 'Standby->Primary' || l_standby_no_info;
960 
961     when C_CONNECT_PRIMARY_TO_STANDBY   then
962 
963          return 'Primary->Standby' || l_standby_no_info;
964 
965     when C_CONNECT_TO_SIMULATED_STANDBY then
966 
967          return 'Simulated Standby' || l_standby_no_info;
968 
969   end case;
970 
971 end;
972 
973 /*==========================================================================*/
974 
975 procedure match_connection_data(p_type number,
976                                 p_rec fnd_adg_control%rowtype,
977                                 p_standby_number number,
978                                 p_link_name varchar2,
979                                 p_link_owner varchar2,
980                                 p_link_connstr varchar2,
981                                 p_type_info varchar2)
982 as
983 l_match_link_owner         varchar2(30) := null;
984 l_match_link_name          varchar2(128) := null;
985 l_match_link_connstr       varchar2(255) := null;
986 begin
987 
988   case p_type
989 
990     when C_CONNECT_STANDBY_TO_PRIMARY then
991 
992          l_match_link_owner   := p_rec.stndby_to_primary_link_owner;
993          l_match_link_name    := p_rec.stndby_to_primary_link_name;
994          l_match_link_connstr := p_rec.stndby_to_primary_connstr;
995 
996     when C_CONNECT_PRIMARY_TO_STANDBY then
997 
998          case p_standby_number
999 
1000            when 1 then
1001                    l_match_link_owner   := p_rec.primary_to_stndby1_link_owner;
1002                    l_match_link_name    := p_rec.primary_to_stndby1_link_name ;
1003                    l_match_link_connstr := p_rec.primary_to_stndby1_connstr   ;
1004            when 2 then
1005                    l_match_link_owner   := p_rec.primary_to_stndby2_link_owner;
1006                    l_match_link_name    := p_rec.primary_to_stndby2_link_name ;
1007                    l_match_link_connstr := p_rec.primary_to_stndby2_connstr   ;
1008            when 3 then
1009                    l_match_link_owner   := p_rec.primary_to_stndby3_link_owner;
1010                    l_match_link_name    := p_rec.primary_to_stndby3_link_name ;
1011                    l_match_link_connstr := p_rec.primary_to_stndby3_connstr   ;
1012            when 4 then
1013                    l_match_link_owner   := p_rec.primary_to_stndby4_link_owner;
1014                    l_match_link_name    := p_rec.primary_to_stndby4_link_name ;
1015                    l_match_link_connstr := p_rec.primary_to_stndby4_connstr   ;
1016            when 5 then
1017                    l_match_link_owner   := p_rec.primary_to_stndby5_link_owner;
1018                    l_match_link_name    := p_rec.primary_to_stndby5_link_name ;
1019                    l_match_link_connstr := p_rec.primary_to_stndby5_connstr   ;
1020 
1021          end case;
1022 
1023     when C_CONNECT_TO_SIMULATED_STANDBY then
1024 
1025          l_match_link_owner   := p_rec.simulated_stndby_link_owner;
1026          l_match_link_name    := p_rec.simulated_stndby_link_name;
1027          l_match_link_connstr := p_rec.simulated_stndby_connstr;
1028 
1029   end case;
1030 
1031   if ( l_match_link_owner   is not null and
1032        l_match_link_name    is not null and
1033        l_match_link_connstr is not null and
1034        (
1035             ( l_match_link_owner = upper(p_link_owner) and
1036               l_match_link_name  = upper(p_link_name) )
1037          or
1038             ( l_match_link_connstr = upper(p_link_connstr) )
1039        )
1040      )
1041   then
1042      fnd_adg_exception.raise_error
1043                (fnd_adg_exception.C_UTLERR_CDATA_EXISTS,p_type_info);
1044   end if;
1045 
1046 end;
1047 
1048 /*==========================================================================*/
1049 
1050 procedure check_connection_data(p_type number,
1051                                 p_rec fnd_adg_control%rowtype,
1052                                 p_standby_number number,
1053                                 p_link_name varchar2,
1054                                 p_link_owner varchar2,
1055                                 p_link_connstr varchar2)
1056 as
1057 l_type_info  varchar2(100);
1058 begin
1059 
1060   l_type_info := get_connection_type_info(p_type,p_standby_number);
1061 
1062   case p_type
1063 
1064     when C_CONNECT_STANDBY_TO_PRIMARY then
1065 
1066 	 null;  -- can be same as simulated standby.
1067 
1068     when C_CONNECT_PRIMARY_TO_STANDBY then
1069 
1070          match_connection_data(C_CONNECT_STANDBY_TO_PRIMARY,p_rec,
1071                                null,p_link_name,p_link_owner,p_link_connstr,
1072                                l_type_info);
1073          match_connection_data(C_CONNECT_TO_SIMULATED_STANDBY,p_rec,
1074                                null,p_link_name,p_link_owner,p_link_connstr,
1075                                l_type_info);
1076 
1077     when C_CONNECT_TO_SIMULATED_STANDBY then
1078 
1079          null;  -- can be same as standby to primary
1080 
1081   end case;
1082 
1083   for i in 1..C_MAX_STANDBY_SYSTEMS loop
1084 
1085     if ( ( p_type <> C_CONNECT_PRIMARY_TO_STANDBY ) or
1086          ( p_type = C_CONNECT_PRIMARY_TO_STANDBY and i <> p_standby_number ) )
1087     then
1088          match_connection_data(C_CONNECT_PRIMARY_TO_STANDBY,p_rec,
1089                                i,p_link_name,p_link_owner,p_link_connstr,
1090                                l_type_info);
1091     end if;
1092   end loop;
1093 
1094 end;
1095 
1096 /*==========================================================================*/
1097 
1098 procedure set_connection_data(p_type number,
1099                               p_rec in out nocopy fnd_adg_control%rowtype,
1100                               p_standby_number number,
1101                               p_link_name varchar2,
1102                               p_link_owner varchar2,
1103                               p_link_connstr varchar2)
1104 as
1105 begin
1106 
1107   case p_type
1108 
1109     when C_CONNECT_STANDBY_TO_PRIMARY then
1110 
1111          p_rec.stndby_to_primary_link_owner := upper(p_link_owner);
1112          p_rec.stndby_to_primary_link_name  := upper(p_link_name);
1113          p_rec.stndby_to_primary_link_valid := 'N';
1114          p_rec.stndby_to_primary_connstr    := upper(p_link_connstr);
1115 
1116     when C_CONNECT_PRIMARY_TO_STANDBY then
1117 
1118          case p_standby_number
1119 
1120            when 1 then
1121                        p_rec.primary_to_stndby1_link_owner:= p_link_owner;
1122                        p_rec.primary_to_stndby1_link_name := p_link_name;
1123                        p_rec.primary_to_stndby1_link_valid:= 'N';
1124                        p_rec.primary_to_stndby1_connstr   := p_link_connstr;
1125            when 2 then
1126                        p_rec.primary_to_stndby2_link_owner:= p_link_owner;
1127                        p_rec.primary_to_stndby2_link_name := p_link_name;
1128                        p_rec.primary_to_stndby2_link_valid:= 'N';
1129                        p_rec.primary_to_stndby2_connstr   := p_link_connstr;
1130            when 3 then
1131                        p_rec.primary_to_stndby3_link_owner:= p_link_owner;
1132                        p_rec.primary_to_stndby3_link_name := p_link_name;
1133                        p_rec.primary_to_stndby3_link_valid:= 'N';
1134                        p_rec.primary_to_stndby3_connstr   := p_link_connstr;
1135            when 4 then
1136                        p_rec.primary_to_stndby4_link_owner:= p_link_owner;
1137                        p_rec.primary_to_stndby4_link_name := p_link_name;
1138                        p_rec.primary_to_stndby4_link_valid:= 'N';
1139                        p_rec.primary_to_stndby4_connstr   := p_link_connstr;
1140            when 5 then
1141                        p_rec.primary_to_stndby5_link_owner:= p_link_owner;
1142                        p_rec.primary_to_stndby5_link_name := p_link_name;
1143                        p_rec.primary_to_stndby5_link_valid:= 'N';
1144                        p_rec.primary_to_stndby5_connstr   := p_link_connstr;
1145          end case;
1146 
1147     when C_CONNECT_TO_SIMULATED_STANDBY then
1148 
1149          p_rec.simulated_stndby_link_owner := upper(p_link_owner);
1150          p_rec.simulated_stndby_link_name  := upper(p_link_name);
1151          p_rec.simulated_stndby_link_valid := 'N';
1152          p_rec.simulated_stndby_connstr    := upper(p_link_connstr);
1153 
1154   end case;
1155 
1156 end;
1157 
1158 /*==========================================================================*/
1159 
1160 procedure check_connection_dbid(p_type number,
1161                                 p_link_name varchar2,
1162                                 p_link_service varchar2,
1163                                 p_type_info varchar2)
1164 as
1165 l_rc number;
1166 l_sid number;
1167 l_serial number;
1168 l_rpc_sid number;
1169 l_rpc_serial number;
1170 l_dbid number;
1171 l_dbname varchar2(30);
1172 l_rpc_dbid number;
1173 l_rpc_dbname varchar2(30);
1174 l_rpc_open_mode varchar2(30);
1175 l_rpc_database_role varchar2(30);
1176 l_sysguid   varchar2(64);
1177 l_rpc_client_info varchar2(64);
1178 l_rpc_service_name varchar2(64);
1179 
1180 begin
1181 
1182   if ( p_link_name is null )
1183   then
1184      fnd_adg_exception.raise_error
1185                (fnd_adg_exception.C_UTLERR_LINKCHK_NULL,p_type_info);
1186   end if;
1187 
1188   begin
1189 
1190     execute immediate 'select 1 from dual@' || p_link_name
1191             into l_rc;
1192 
1193   exception
1194     when others then
1195 
1196        fnd_adg_exception.raise_error
1197                (fnd_adg_exception.C_UTLERR_LINKCHK_TNS,
1198                     p_type_info||' '||sqlerrm);
1199 
1200   end;
1201 
1202 	/* Get my sid/serial */
1203 
1204   select a.sid,a.serial#
1205     into l_sid,l_serial
1206     from v$session a
1207    where a.sid = ( select distinct b.sid from v$mystat b);
1208 
1209 	/* Make sure not a loopback session */
1210 
1211    execute immediate
1212            'select a.sid,a.serial#,a.service_name ' ||
1213            '  from v$session@'||p_link_name|| ' a' ||
1214            ' where a.sid = ' ||
1215                ' ( select distinct b.sid from v$mystat@'||p_link_name||' b)'
1216       into l_rpc_sid,l_rpc_serial,l_rpc_service_name;
1217 
1218 /*
1219    sys.dbms_output.put_line
1220 	( 'l_sid='||l_sid || ' l_serial='|| l_serial ||
1221           ' l_rpc_sid='|| l_rpc_sid || ' l_rpc_serial='|| l_rpc_serial);
1222 */
1223 
1224    if ( l_sid = l_rpc_sid and
1225         l_serial = l_rpc_serial )
1226    then
1227       fnd_adg_exception.raise_error
1228                (fnd_adg_exception.C_UTLERR_LINKCHK_LOOPBACK,
1229                                   p_type_info);
1230    end if;
1231 
1232 	/* Make sure same dbid,name */
1233 
1234    select a.dbid,a.name
1235      into l_dbid,l_dbname
1236      from v$database a;
1237 
1238    execute immediate
1239            'select a.dbid,a.name,a.open_mode,a.database_role ' ||
1240            '  from v$database@'||p_link_name|| ' a'
1241       into l_rpc_dbid,l_rpc_dbname,l_rpc_open_mode,l_rpc_database_role;
1242 
1243    if ( l_dbid <> l_rpc_dbid or
1244         l_dbname <> l_rpc_dbname )
1245    then
1246       fnd_adg_exception.raise_error
1247                (fnd_adg_exception.C_UTLERR_LINKCHK_BAD_DBID,
1248                 p_type_info||' This DBID/Name '|| l_dbid || '-' || l_dbname
1249                            ||' RPC DBID/Name '|| l_rpc_dbid||'-'|| l_rpc_dbname
1250                );
1251    end if;
1252 
1253 	/* Handle read-write/read only */
1254 
1255    if ( p_type = C_CONNECT_STANDBY_TO_PRIMARY or
1256         p_type = C_CONNECT_TO_SIMULATED_STANDBY )
1257    then
1258 	/* Make sure rpc is primary and read write. */
1259 
1260       if ( l_rpc_open_mode <> C_OPEN_READ_WRITE or
1261            l_rpc_database_role <> C_PRIMARY_ROLE )
1262       then
1263          fnd_adg_exception.raise_error
1264                   (fnd_adg_exception.C_UTLERR_LINKCHK_BAD_DB_ROLE,
1265                    p_type_info);
1266       end if;
1267 
1268 	/* And just in case db is a clone with same dbid see if remote
1269 	   can find my session.
1270 	*/
1271 
1272       l_sysguid := rawtohex(sys_guid);
1273 
1274       DBMS_APPLICATION_INFO.SET_CLIENT_INFO(l_sysguid);
1275 
1276       begin
1277 
1278          execute immediate
1279                  'select a.client_info ' ||
1280                  '  from v$session@'||p_link_name|| ' a' ||
1281                  ' where a.sid = ' || l_sid ||
1282                  '   and a.serial# = ' || l_serial
1283             into l_rpc_client_info;
1284 
1285       exception
1286         when no_data_found then
1287 
1288              l_rpc_client_info := null;
1289       end;
1290 
1291       if ( l_rpc_client_info is null or
1292            l_rpc_client_info <> l_sysguid )
1293       then
1294          fnd_adg_exception.raise_error
1295                   (fnd_adg_exception.C_UTLERR_LINKCHK_RPC_IS_CLONE,
1296                    p_type_info);
1297       end if;
1298 
1299    end if;
1300 
1301    if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
1302    then
1303 
1304         /* Make sure rpc is standby and read only. */
1305 
1306       if ( l_rpc_open_mode  <> C_OPEN_READ_ONLY or
1307            l_rpc_database_role <> C_STANDBY_ROLE )
1308       then
1309          fnd_adg_exception.raise_error
1310                   (fnd_adg_exception.C_UTLERR_LINKCHK_BAD_STANDBY,
1311                    p_type_info);
1312       end if;
1313 
1314    end if;
1315 
1316    if ( p_type = C_CONNECT_TO_SIMULATED_STANDBY )
1317    then
1318         /* Make sure matching service */
1319 
1320       if ( p_link_service is null or l_rpc_service_name is null or
1321            upper(p_link_service) <> upper(l_rpc_service_name) )
1322       then
1323          fnd_adg_exception.raise_error
1324                   (fnd_adg_exception.C_UTLERR_LINKCHK_BAD_SERVICE,
1325                    p_type_info);
1326       end if;
1327 
1328    end if;
1329 
1330 end;
1331 
1332 /*==========================================================================*/
1333 
1334 procedure do_process_adg_violations(p_logoff boolean,
1335                                     p_application_id number default null,
1336                                     p_concurrent_program_id number default null)
1337 as
1338 l_rec fnd_adg_control%rowtype;
1339 
1340 l_adg_violations number := 0;
1341 l_trace_fno utl_file.file_type;
1342 l_trace_rec varchar2(32767);
1343 l_idx number;
1344 l_file_name varchar2(1000) := null ;
1345 l_directory_path varchar2(4000);
1346 l_trace_error number := 0;
1347 l_tracefile	varchar2(2048) := null;
1348 l_magic_switch_enabled boolean;
1349 l_request_id number;
1350 
1351 begin
1352 
1353   l_magic_switch_enabled := G_MAGIC_SWITCH_ENABLED;
1354 
1355   disable_violation_trace;
1356 
1357 	-- Cannot do DML if on standby  - but we can't just check
1358 	-- is_standby as in simulation mode it will return true on primary.
1359 	-- Use is_true_standby
1360 
1361   if ( fnd_adg_support.is_true_standby )
1362   then
1363      return;
1364   end if;
1365 
1366   if ( not is_session_simulated_standby )
1367   then
1368      return;
1369   end if;
1370 
1371   l_rec := get_adg_control;
1372 
1373   if ( l_rec.simulated_stndby_trc_dir_obj is null )
1374   then
1375      return;
1376   end if;
1377 
1378   begin
1379 
1380     select DIRECTORY_PATH
1381       into l_directory_path
1382       from all_directories
1383      where DIRECTORY_NAME=l_rec.simulated_stndby_trc_dir_obj
1384        and owner= 'SYS';
1385 
1386   exception
1387 
1388      when others then
1389               return;
1390 
1391   end;
1392 
1393         -- Backwards compatibility - tracefile doesn't exit in 10g.
1394 
1395   l_tracefile := null;
1396 
1397   begin
1398 
1399     execute immediate
1400              'select b.tracefile ' ||
1401              '   from v$session a,v$process b ' ||
1402              '  where a.sid = ( select distinct c.sid from v$mystat c) ' ||
1403              '    and a.paddr = b.addr'
1404        into l_tracefile;
1405 
1406   exception
1407      when no_data_found then
1408 
1409           l_tracefile := null;
1410   end;
1411 
1412   if ( l_tracefile is null )
1413   then
1414      return;
1415   end if;
1416 
1417   l_trace_error := 0;
1418 
1419   begin
1420 
1421     l_file_name := substr(l_tracefile,length(l_directory_path)+2);
1422 
1423     l_trace_fno := utl_file.fopen(l_rec.simulated_stndby_trc_dir_obj,
1424                                   l_file_name,'r',32767);
1425 
1426   exception
1427      when utl_file.INVALID_PATH
1428         then
1429            l_trace_error := 1;
1430      when utl_file.INVALID_OPERATION
1431         then
1432            l_trace_error := 2;
1433      when others
1434         then
1435            l_trace_error := 3;
1436   end;
1437 
1438   if ( l_trace_error = 0 )
1439   then
1440 
1441     loop
1442 
1443       begin
1444 
1445         utl_file.get_line(l_trace_fno,l_trace_rec);
1446 
1447       exception
1448         when no_data_found
1449              then exit;
1450 
1451         when others
1452              then
1453                 l_trace_error := 4;
1454                 exit;
1455       end;
1456 
1457       l_idx := instr(l_trace_rec,C_MAGIC_SWITCH_IDENT);
1458 
1459       -- if ( l_idx > 0 )
1460       if ( l_idx = 1 )
1461       then
1462          l_adg_violations := l_adg_violations + 1;
1463       end if;
1464 
1465     end loop;
1466 
1467     utl_file.fclose(l_trace_fno);
1468 
1469   end if;
1470 
1471   begin
1472     l_request_id := fnd_global.conc_request_id;
1473   exception
1474     when others then
1475          l_request_id := null;
1476   end;
1477 
1478   log_adg_violations(l_request_id,l_adg_violations,
1479                      boolean_to_yn(l_magic_switch_enabled),
1480                      l_file_name, l_trace_error);
1481 
1482   if ( l_adg_violations > get_simulation_error_threshold )
1483   then
1484      if ( p_logoff )
1485      then
1486         fnd_adg_support.handle_standby_error
1487                      (l_request_id,true,p_logoff,
1488                       l_adg_violations - get_simulation_error_threshold);
1489      else
1490         fnd_adg_support.handle_standby_error
1491                      (p_application_id,p_concurrent_program_id,true,p_logoff,
1492                       l_adg_violations - get_simulation_error_threshold);
1493      end if;
1494   end if;
1495 
1496 end;
1497 
1498 /*==========================================================================*/
1499 
1500 procedure set_database_triggers(p_enable boolean)
1501 as
1502 cursor c1 is select a.status,a.object_name
1503                from dba_objects a
1504               where a.owner = user
1505                 and a.object_name in
1506                         ( C_ERROR_TRIGGER,C_LOGON_TRIGGER,C_LOGOFF_TRIGGER )
1507                 and a.object_type = 'TRIGGER';
1508 
1509 begin
1510 
1511   check_standby_support;
1512 
1513   check_rpc_state(true);
1514   check_adg_state(false);
1515 
1516   for f_rec in c1 loop
1517 
1518     if ( not p_enable )
1519     then
1520        execute immediate 'alter trigger ' || f_rec.object_name || ' disable';
1521     else
1522        if ( f_rec.status = 'INVALID' )
1523        then
1524           execute immediate 'alter trigger ' || f_rec.object_name || ' compile';
1525        end if;
1526 
1527        execute immediate 'alter trigger ' || f_rec.object_name || ' enable';
1528     end if;
1529 
1530   end loop;
1531 
1532 end;
1533 
1534 /*==========================================================================*/
1535 /*==================== Start of public methods =============================*/
1536 /*==========================================================================*/
1537 
1538 /*==========================================================================*/
1539 
1540 function get_program_access_code return number
1541 as
1542 l_access_code number;
1543 begin
1544 
1545   l_access_code := G_CONC_PROGRAM_ACCESS_CODE;
1546 
1547   set_program_access_code;
1548 
1549   return l_access_code;
1550 
1551 end;
1552 
1553 /*==========================================================================*/
1554 
1555 procedure clone_clean(p_commit boolean default true)
1556 as
1557 l_rec fnd_adg_control%rowtype;
1558 begin
1559 
1560         -- Dummy get_adg_control. This ensure that on a fresh system
1561         -- we auto_init the required data.
1562 
1563   l_rec := get_adg_control;
1564 
1565   check_standby_support;
1566 
1567   delete from fnd_adg_commit_wait;
1568   delete from fnd_adg_simulated_stndby_trc;
1569 
1570   clean_adg_control(false);
1571 
1572   if ( p_commit )
1573   then
1574      commit;
1575   end if;
1576 
1577 end;
1578 
1579 /*==========================================================================*/
1580 
1581 procedure clean_all(p_commit boolean default true)
1582 as
1583 l_rec fnd_adg_control%rowtype;
1584 begin
1585 
1586         -- Dummy get_adg_control. This ensure that on a fresh system
1587         -- we auto_init the required data.
1588 
1589   l_rec := get_adg_control;
1590 
1591   check_standby_support;
1592 
1593   clean_adg_control(false,true,true);
1594 
1595   delete from fnd_adg_concurrent_program;
1596 
1597   clone_clean(false);
1598 
1599   fnd_adg_object.init_package_list;
1600 
1601   if ( p_commit )
1602   then
1603      commit;
1604   end if;
1605 
1606 end;
1607 
1608 /*==========================================================================*/
1609 
1610 function get_standby_to_primary_dblink return varchar2
1611 as
1612 l_rec fnd_adg_control%rowtype;
1613 begin
1614 
1615   check_standby_support;
1616 
1617   l_rec := get_adg_control;
1618 
1619   return l_rec.stndby_to_primary_link_name;
1620 
1621 end;
1622 
1623 /*==========================================================================*/
1624 
1625 function is_connection_registered(p_connstr varchar2,
1626                                   p_check_valid boolean default false,
1627                                   p_check_available boolean default false)
1628               return boolean
1629 as
1630 l_rec fnd_adg_control%rowtype;
1631 
1632 l_standby_number number := 0;
1633 l_found_connection boolean := false;
1634 
1635 l_link_name varchar2(128);
1636 l_link_owner varchar2(30);
1637 l_link_valid varchar2(10);
1638 l_connstr    varchar2(255);
1639 l_link_service varchar2(64);
1640 l_connection_type number;
1641 
1642 l_type_info  varchar2(255);
1643 
1644 begin
1645 
1646   if ( not is_standby_access_supported )
1647   then
1648      return false;
1649   end if;
1650 
1651   if ( p_connstr is null )
1652   then
1653      return false;
1654   end if;
1655 
1656   l_rec := get_adg_control;
1657 
1658   for i in 1..C_MAX_STANDBY_SYSTEMS loop
1659 
1660     if ( find_primary_to_standby(l_rec,i,upper(p_connstr)) )
1661     then
1662        l_standby_number := i;
1663        l_connection_type := C_CONNECT_PRIMARY_TO_STANDBY;
1664        l_found_connection := true;
1665        exit;
1666     end if;
1667 
1668   end loop;
1669 
1670   if ( not l_found_connection )
1671   then
1672      l_connection_type := C_CONNECT_TO_SIMULATED_STANDBY;
1673   end if;
1674 
1675   get_connection_data(l_connection_type,l_rec,l_standby_number,
1676                       l_link_name,l_link_owner,l_connstr,l_link_valid,
1677                       l_link_service);
1678 
1679   if ( l_connstr is null )
1680   then
1681      return false;
1682   end if;
1683 
1684   if ( l_connstr <> upper(p_connstr) )
1685   then
1686      return false;
1687   end if;
1688 
1689   if ( not p_check_valid and not p_check_available )
1690   then
1691      return true;
1692   end if;
1693 
1694 	-- No point going any further if ADG support has not been enabled.
1695 
1696   if ( not is_adg_support_enabled )
1697   then
1698      return false;
1699   end if;
1700 
1701   if ( p_check_valid and not yn_to_boolean(l_link_valid) )
1702   then
1703      return false;
1704   end if;
1705 
1706   if ( p_check_available )
1707   then
1708 
1709      l_type_info := get_connection_type_info(l_connection_type,
1710                                              l_standby_number);
1711 
1712      begin
1713 
1714        check_connection_dbid(l_connection_type,l_link_name,l_link_service,
1715                              l_type_info);
1716      exception
1717          when others then
1718               return false;
1719      end;
1720 
1721   end if;
1722 
1723   return true;
1724 
1725 end;
1726 
1727 /*==========================================================================*/
1728 
1729 procedure find_registered_standby(p_connstr varchar2,
1730                                   p_exists  out nocopy boolean,
1731                                   p_valid   out nocopy boolean,
1732                                   p_req_class_app_id out nocopy number,
1733                                   p_req_class_id out nocopy number
1734                                  )
1735 as
1736 l_rec fnd_adg_control%rowtype;
1737 l_link_name varchar2(128);
1738 l_link_owner varchar2(30);
1739 l_link_valid varchar2(10);
1740 l_connstr    varchar2(255);
1741 l_link_service varchar2(64);
1742 l_req_class_app_id number;
1743 l_req_class_id number;
1744 begin
1745 
1746        -- No rpc checks as should only call be embedded clients. And
1747        -- it's just a look up.
1748 
1749   p_exists := false;
1750   p_valid  := false;
1751   p_req_class_app_id := -1;
1752   p_req_class_id := -1;
1753 
1754   if ( p_connstr is null )
1755   then
1756      return;
1757   end if;
1758 
1759   l_rec := get_adg_control;
1760 
1761   for i in 1..C_MAX_STANDBY_SYSTEMS loop
1762 
1763     if ( find_primary_to_standby(l_rec,i,upper(p_connstr)) )
1764     then
1765 
1766        p_exists := true;
1767 
1768        get_connection_data(C_CONNECT_PRIMARY_TO_STANDBY,l_rec,i,
1769                            l_link_name,l_link_owner,l_connstr,l_link_valid,
1770                            l_link_service);
1771 
1772        p_valid  := yn_to_boolean(l_link_valid);
1773 
1774        get_cm_class_data(l_rec,i,l_req_class_app_id,l_req_class_id);
1775 
1776        p_req_class_app_id := l_req_class_app_id;
1777        p_req_class_id     := l_req_class_id;
1778 
1779        return;
1780 
1781     end if;
1782 
1783   end loop;
1784 
1785   return;
1786 
1787 end;
1788 
1789 /*==========================================================================*/
1790 
1791 procedure register_connection(p_type number,
1792                               p_link_name varchar2,
1793                               p_link_owner varchar2 default 'PUBLIC',
1794                               p_link_connstr varchar2 default null,
1795                               p_create_db_link_if_undefined
1796                                                boolean default false,
1797                               p_standby_number number default null
1798                              )
1799 as
1800 cursor c1 is select a.owner,a.DB_LINK,upper(a.host) host
1801                from all_db_links a
1802               where a.owner = upper(p_link_owner)
1803                 and a.DB_LINK = upper(p_link_name);
1804 
1805 l_rec fnd_adg_control%rowtype;
1806 
1807 found_db_link boolean := false;
1808 l_connstr1 varchar2(2000);
1809 
1810 begin
1811 
1812   check_standby_support;
1813   check_connection_type(p_type);
1814 
1815 	-- RPC state can be on or off except for standby_to_primary.
1816 
1817   if ( p_type = C_CONNECT_STANDBY_TO_PRIMARY )
1818   then
1819      check_rpc_state(false);
1820   end if;
1821 
1822   check_adg_state(false);
1823 
1824   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
1825   then
1826      check_standby_number(p_standby_number);
1827   end if;
1828 
1829   if ( p_link_owner is null or p_link_name is null )
1830   then
1831      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_REGISTER_LINK_IS_NULL);
1832   end if;
1833 
1834   if ( C_FORCE_PUBLIC_DBLINK and upper(p_link_owner) <> 'PUBLIC' )
1835   then
1836      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_OWNER_NOT_PUBLIC);
1837   end if;
1838 
1839   if ( p_link_connstr is not null and
1840        length(p_link_connstr) > C_MAX_CONNSTR_LENGTH )
1841   then
1842      fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_CONNSTR_TOO_LONG,
1843                                    to_char(C_MAX_CONNSTR_LENGTH));
1844   end if;
1845 
1846   l_rec := get_adg_control;
1847 
1848   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
1849   then
1850      if ( l_rec.stndby_to_primary_link_owner is not null and
1851           l_rec.stndby_to_primary_link_name is not null and
1852           l_rec.stndby_to_primary_link_owner = upper(p_link_owner) and
1853           l_rec.stndby_to_primary_link_name = upper(p_link_name) )
1854      then
1855         fnd_adg_exception.raise_error
1856              (fnd_adg_exception.C_UTLERR_STDBY_P_LINKS_MATCH,p_link_name);
1857      end if;
1858   end if;
1859 
1860   check_connection_data(p_type,l_rec,p_standby_number,
1861                         p_link_name,p_link_owner,p_link_connstr);
1862 
1863   for f_rec in c1 loop
1864 
1865     found_db_link := true;
1866 
1867     if ( p_link_connstr is not null and
1868          f_rec.host <> upper(p_link_connstr) )
1869     then
1870        fnd_adg_exception.raise_error
1871                         (fnd_adg_exception.C_UTLERR_LINK_HOST_MISMATCH,
1872                          p_link_name );
1873     end if;
1874 
1875     l_connstr1 := f_rec.host;
1876 
1877     exit;
1878 
1879   end loop;
1880 
1881   if ( not found_db_link )
1882   then
1883      if ( p_create_db_link_if_undefined and C_FORCE_PUBLIC_DBLINK  and
1884           p_link_connstr is not null )
1885      then
1886         execute immediate
1887            ' create public database link ' || p_link_name || ' using ' ||
1888               '''' || upper(p_link_connstr) || '''' ;
1889 
1890         l_connstr1 := upper(p_link_connstr);
1891      else
1892         fnd_adg_exception.raise_error(fnd_adg_exception.C_UTLERR_REG_LINK_NOT_FOUND,
1893                                    p_link_name );
1894      end if;
1895   end if;
1896 
1897   l_rec := get_and_lock_adg_control;
1898 
1899   set_connection_data(p_type,l_rec,p_standby_number,
1900                       upper(p_link_name),upper(p_link_owner),
1901                       upper(l_connstr1));
1902 
1903   update_adg_control(l_rec);
1904 
1905 end;
1906 
1907 /*==========================================================================*/
1908 
1909 procedure clear_connection(p_type number,
1910                            p_standby_number number default null)
1911 as
1912 l_rec fnd_adg_control%rowtype;
1913 
1914 found_db_link boolean := false;
1915 l_connstr1 varchar2(2000);
1916 
1917 begin
1918 
1919   check_standby_support;
1920   check_connection_type(p_type);
1921 
1922         -- RPC state can be on or off except for standby_to_primary.
1923 
1924   if ( p_type = C_CONNECT_STANDBY_TO_PRIMARY )
1925   then
1926      check_rpc_state(false);
1927   end if;
1928 
1929   check_adg_state(false);
1930 
1931   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
1932   then
1933      if ( p_standby_number is not null ) -- allow for cleanup of all entries
1934      then
1935         check_standby_number(p_standby_number);
1936      end if;
1937   end if;
1938 
1939   l_rec := get_and_lock_adg_control;
1940 
1941   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY and p_standby_number is null )
1942   then
1943      for i in 1..C_MAX_STANDBY_SYSTEMS loop
1944 
1945        set_connection_data(p_type,l_rec,i,null,null,null);
1946 
1947      end loop;
1948   else
1949      set_connection_data(p_type,l_rec,p_standby_number,null,null,null);
1950   end if;
1951 
1952   update_adg_control(l_rec);
1953 
1954 end;
1955 
1956 /*==========================================================================*/
1957 
1958 procedure validate_connection(p_type number,
1959                               p_standby_number number default null)
1960 as
1961 l_rec fnd_adg_control%rowtype;
1962 l_link_name varchar2(128);
1963 l_link_owner varchar2(30);
1964 l_link_valid varchar2(10);
1965 l_connstr    varchar2(255);
1966 l_link_service varchar2(64);
1967 l_type_info  varchar2(255);
1968 
1969 begin
1970 
1971   check_standby_support;
1972   check_connection_type(p_type);
1973 
1974         -- RPC state can be on or off except for standby_to_primary.
1975 
1976   if ( p_type = C_CONNECT_STANDBY_TO_PRIMARY )
1977   then
1978      check_rpc_state(false);
1979   end if;
1980 
1981   check_adg_state(false);
1982 
1983   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
1984   then
1985      check_standby_number(p_standby_number);
1986   end if;
1987 
1988 	-- First mark as invalid in case of errors.
1989 
1990   l_rec := get_and_lock_adg_control;
1991 
1992   set_connection_valid(p_type,l_rec,false,p_standby_number);
1993 
1994   update_adg_control(l_rec);
1995 
1996   l_rec := get_adg_control;
1997 
1998   get_connection_data(p_type,l_rec,p_standby_number,
1999                       l_link_name,l_link_owner,l_connstr,l_link_valid,
2000                       l_link_service);
2001 
2002   l_type_info := get_connection_type_info(p_type,p_standby_number);
2003 
2004   check_connection_dbid(p_type,l_link_name,l_link_service,l_type_info);
2005 
2006   l_rec := get_and_lock_adg_control;
2007 
2008   set_connection_valid(p_type,l_rec,true,p_standby_number);
2009 
2010   update_adg_control(l_rec);
2011 
2012 end;
2013 
2014 /*==========================================================================*/
2015 
2016 procedure get_connection_data(p_type number,
2017                               p_valid out nocopy boolean,
2018                               p_connstr out nocopy varchar2,
2019                               p_standby_number number default null
2020                              )
2021 as
2022 l_rec fnd_adg_control%rowtype;
2023 l_link_name varchar2(128);
2024 l_link_owner varchar2(30);
2025 l_link_valid varchar2(10);
2026 l_connstr    varchar2(255);
2027 l_link_service varchar2(64);
2028 begin
2029 
2030 	-- No rpc checks as should only call be embedded clients. And
2031         -- it's just a look up.
2032 
2033   check_connection_type(p_type);
2034 
2035   if ( p_type = C_CONNECT_PRIMARY_TO_STANDBY )
2036   then
2037      if ( p_standby_number is not null )
2038      then
2039         check_standby_number(p_standby_number);
2040      end if;
2041   end if;
2042 
2043   l_rec := get_adg_control;
2044 
2045   get_connection_data(p_type,l_rec,p_standby_number,
2046                       l_link_name,l_link_owner,l_connstr,l_link_valid,
2047                       l_link_service);
2048 
2049 
2050   p_valid := yn_to_boolean(l_link_valid);
2051   p_connstr := l_connstr;
2052 
2053 end;
2054 
2055 /*==========================================================================*/
2056 
2057 procedure get_standby_cm_class       (p_standby_number number,
2058                                       p_req_class_app_id out nocopy number,
2059                                       p_req_class_id out nocopy number )
2060 as
2061 l_rec fnd_adg_control%rowtype;
2062 begin
2063 
2064   check_standby_number(p_standby_number);
2065 
2066   l_rec := get_adg_control;
2067 
2068   p_req_class_app_id := null;
2069   p_req_class_id := null;
2070 
2071   get_cm_class_data(l_rec,p_standby_number,p_req_class_app_id,p_req_class_id);
2072 
2073 end;
2074 
2075 /*==========================================================================*/
2076 
2077 procedure register_standby_cm_class  (p_standby_number number,
2078                                       p_req_class_app_id number,
2079                                       p_req_class_id number )
2080 as
2081 l_rec fnd_adg_control%rowtype;
2082 begin
2083 
2084   check_standby_support;
2085 
2086         -- RPC state can be on or off
2087 
2088   check_adg_state(false);
2089 
2090   check_standby_number(p_standby_number);
2091 
2092   if ( not is_standby_manager_defined(p_req_class_app_id,p_req_class_id,false))
2093   then
2094      fnd_adg_exception.raise_error
2095                           (fnd_adg_exception.C_UTLERR_REG_CM_NOT_DEFINED);
2096   end if;
2097 
2098   l_rec := get_and_lock_adg_control;
2099 
2100   set_cm_class_data(l_rec,p_standby_number,p_req_class_app_id,p_req_class_id);
2101 
2102   update_adg_control(l_rec);
2103 
2104 end;
2105 
2106 /*==========================================================================*/
2107 
2108 procedure set_simulated_standby_options(p_enable_simulated_standby
2109                                                boolean default null,
2110                                         p_enable_auto_simulation
2111                                                boolean default null,
2112                                         p_simulated_standby_service
2113                                                  varchar2 default null,
2114                                         p_simulation_error_threshold
2115                                                  number default null,
2116                                         p_trace_directory_obj
2117                                                  varchar2 default null)
2118 
2119 as
2120 l_rec fnd_adg_control%rowtype;
2121 l_dir_obj_ok number;
2122 begin
2123 
2124   check_standby_support;
2125 
2126        -- RPC state can be on or off as we're just registering options.
2127 
2128   check_adg_state(false);
2129 
2130   l_rec := get_and_lock_adg_control;
2131 
2132   if ( p_enable_auto_simulation is not null )
2133   then
2134      l_rec.enable_auto_simulated_standby :=
2135                   boolean_to_yn(p_enable_auto_simulation);
2136   end if;
2137 
2138   if ( p_simulated_standby_service is not null )
2139   then
2140      l_rec.simulated_standby_service := upper(p_simulated_standby_service);
2141   end if;
2142 
2143   if ( p_enable_simulated_standby is not null )
2144   then
2145      l_rec.enable_simulated_standby :=
2146                    boolean_to_yn(p_enable_simulated_standby);
2147   end if;
2148 
2149   if ( p_trace_directory_obj is not null )
2150   then
2151 
2152      select count(*)
2153        into l_dir_obj_ok
2154        from all_directories
2155       where DIRECTORY_NAME=p_trace_directory_obj
2156         and owner= 'SYS';
2157 
2158      if ( l_dir_obj_ok <> 1 )
2159      then
2160         fnd_adg_exception.raise_error
2161 			(fnd_adg_exception.C_UTLERR_BAD_DIR_OBJECT);
2162      end if;
2163 
2164      l_rec.simulated_stndby_trc_dir_obj := p_trace_directory_obj;
2165 
2166   end if;
2167 
2168   if ( p_simulation_error_threshold is not null )
2169   then
2170      if ( p_simulation_error_threshold >= C_MIN_ERROR_THRESHOLD and
2171           p_simulation_error_threshold <= C_MAX_ERROR_THRESHOLD )
2172      then
2173         l_rec.simulation_error_threshold := p_simulation_error_threshold;
2174      end if;
2175   end if;
2176 
2177   update_adg_control(l_rec);
2178 
2179 end;
2180 
2181 /*==========================================================================*/
2182 
2183 procedure validate_adg_support(p_no_standby_systems number default null)
2184 as
2185 l_rec fnd_adg_control%rowtype;
2186 l_no_standby_systems number;
2187 begin
2188 
2189   check_standby_support;
2190 
2191   check_rpc_state(false);
2192   check_adg_state(false);
2193 
2194   if ( p_no_standby_systems is null )
2195   then
2196      l_no_standby_systems := C_MAX_STANDBY_SYSTEMS;
2197   else
2198      l_no_standby_systems := p_no_standby_systems;
2199   end if;
2200 
2201   if ( l_no_standby_systems <> 0 )
2202   then
2203      check_standby_number(l_no_standby_systems);
2204   end if;
2205 
2206   validate_connection(fnd_adg_utility.C_CONNECT_STANDBY_TO_PRIMARY);
2207   validate_connection(fnd_adg_utility.C_CONNECT_TO_SIMULATED_STANDBY);
2208 
2209   for i in 1..l_no_standby_systems loop
2210 
2211     validate_connection(fnd_adg_utility.C_CONNECT_PRIMARY_TO_STANDBY,i);
2212 
2213   end loop;
2214 
2215 end;
2216 
2217 /*==========================================================================*/
2218 
2219 procedure enable_adg_support
2220 as
2221 l_rec fnd_adg_control%rowtype;
2222 begin
2223 
2224   check_standby_support;
2225 
2226   check_rpc_state(true);
2227   check_adg_state(false);
2228 
2229 	-- Check that RPC packages are enabled and being used.
2230 
2231   fnd_adg_object.validate_package_usage(true);
2232 
2233   l_rec := get_and_lock_adg_control;
2234 
2235   set_rpc_state(l_rec,C_RPC_ADG_ENABLED);
2236 
2237   l_rec.enable_adg_support := 'Y';
2238 
2239   update_adg_control(l_rec);
2240 
2241 end;
2242 
2243 /*==========================================================================*/
2244 
2245 procedure disable_adg_support
2246 as
2247 l_rec fnd_adg_control%rowtype;
2248 begin
2249 
2250   check_standby_support;
2251 
2252   check_rpc_state(true);
2253   check_adg_state(true);
2254 
2255   l_rec := get_and_lock_adg_control;
2256 
2257   clr_rpc_state(l_rec,C_RPC_ADG_ENABLED);
2258 
2259   l_rec.enable_adg_support := 'N';
2260 
2261   update_adg_control(l_rec);
2262 
2263 end;
2264 
2265 /*==========================================================================*/
2266 
2267 procedure set_control_options(p_enable_commit_wait boolean default null,
2268                               p_max_commit_wait_time number default null,
2269                               p_runtime_validate_timestamp
2270                                                      boolean default null,
2271                               p_always_collect_primary_data
2272                                                      boolean default null,
2273                               p_enable_redirect_if_valid
2274                                                      boolean default null,
2275                               p_enable_standby_error_checks
2276                                                      boolean default null,
2277                               p_enable_automatic_redirection
2278                                                      boolean default null,
2279                               p_standby_error_threshold number default null,
2280                               p_debug_rpc number default null,
2281                               p_debug_slave_rpc number default null
2282                              )
2283 as
2284 l_rec fnd_adg_control%rowtype;
2285 begin
2286 
2287   check_standby_support;
2288 
2289        -- RPC state can be on or off as we're just registering options.
2290 
2291   check_adg_state(false);
2292 
2293   l_rec := get_and_lock_adg_control;
2294 
2295   if ( p_enable_commit_wait is not null )
2296   then
2297      l_rec.enable_commit_wait := boolean_to_yn(p_enable_commit_wait);
2298   end if;
2299 
2300   if ( p_max_commit_wait_time is not null )
2301   then
2302      if ( p_max_commit_wait_time >= 1 and
2303           p_max_commit_wait_time <= C_MAX_COMMIT_WAIT_TIME )
2304      then
2305         l_rec.max_commit_wait_time := p_max_commit_wait_time;
2306      end if;
2307   end if;
2308 
2309   if ( p_debug_rpc is not null )
2310   then
2311      l_rec.debug_rpc := p_debug_rpc;
2312   end if;
2313 
2314   if ( p_debug_slave_rpc is not null )
2315   then
2316      l_rec.debug_slave_rpc := p_debug_slave_rpc;
2317   end if;
2318 
2319   if ( p_runtime_validate_timestamp is not null )
2320   then
2321      l_rec.runtime_validate_timestamp
2322                  := boolean_to_yn(p_runtime_validate_timestamp);
2323   end if;
2324 
2325   if ( p_always_collect_primary_data is not null )
2326   then
2327      l_rec.always_collect_primary_data
2328                  := boolean_to_yn(p_always_collect_primary_data);
2329   end if;
2330 
2331   if ( p_enable_redirect_if_valid is not null )
2332   then
2333      l_rec.enable_redirect_if_valid
2334                  := boolean_to_yn(p_enable_redirect_if_valid);
2335   end if;
2336 
2337   if ( p_enable_standby_error_checks is not null )
2338   then
2339       l_rec.enable_standby_error_checks
2340                      := boolean_to_yn(p_enable_standby_error_checks);
2341   end if;
2342 
2343   if ( p_enable_automatic_redirection is not null )
2344   then
2345       l_rec.enable_automatic_redirection
2346                      := boolean_to_yn(p_enable_automatic_redirection);
2347   end if;
2348 
2349   if ( p_standby_error_threshold is not null )
2350   then
2351      if ( p_standby_error_threshold >= C_MIN_ERROR_THRESHOLD and
2352           p_standby_error_threshold <= C_MAX_ERROR_THRESHOLD )
2353      then
2354         l_rec.standby_error_threshold := p_standby_error_threshold;
2355      end if;
2356   end if;
2357 
2358   update_adg_control(l_rec);
2359 
2360 end;
2361 
2362 /*==========================================================================*/
2363 
2364 procedure prepare_for_rpc_system
2365 as
2366 l_rec fnd_adg_control%rowtype;
2367 begin
2368 
2369   check_standby_support;
2370 
2371   check_rpc_state(false);
2372   check_adg_state(false);
2373 
2374         -- First mark as invalid in case of errors.
2375 
2376   l_rec := get_and_lock_adg_control;
2377 
2378   clr_rpc_state(l_rec,C_RPC_SYSTEM_PREPARED);
2379 
2380   update_adg_control(l_rec);
2381 
2382 	-- Build/compile
2383 
2384   fnd_adg_object.build_all_packages;
2385 
2386 	-- Build remote synonyms - may be null but they will be
2387 	-- rebuilt during switch.
2388 	-- Otherwise first time through compile will fail.
2389 
2390   fnd_adg_object.build_all_synonyms;
2391 
2392   fnd_adg_object.compile_all_packages;
2393 
2394   l_rec := get_and_lock_adg_control;
2395 
2396   set_rpc_state(l_rec,C_RPC_SYSTEM_PREPARED);
2397 
2398   update_adg_control(l_rec);
2399 
2400 end;
2401 
2402 /*==========================================================================*/
2403 
2404 procedure switch_rpc_system_on
2405 as
2406 l_rec fnd_adg_control%rowtype;
2407 begin
2408 
2409   check_standby_support;
2410 
2411   check_rpc_state(false);
2412   check_adg_state(false);
2413 
2414   l_rec := get_adg_control;
2415 
2416   if ( not is_rpc_state(l_rec,C_RPC_SYSTEM_PREPARED) )
2417   then
2418      fnd_adg_exception.raise_error
2419                  (fnd_adg_exception.C_UTLERR_RPC_SYSTEM_NOT_PREPED);
2420   end if;
2421 
2422 	-- Check that RPC packages are using adg_compile_directive.
2423 	-- Best guess pre-enable that packages are the correct version.
2424 
2425   fnd_adg_object.validate_package_usage(false);
2426 
2427 	-- Validate connection
2428 
2429   validate_connection(C_CONNECT_STANDBY_TO_PRIMARY);
2430 
2431   l_rec := get_adg_control;
2432 
2433   if ( not yn_to_boolean(l_rec.stndby_to_primary_link_valid) )
2434   then
2435      fnd_adg_exception.raise_error
2436                  (fnd_adg_exception.C_UTLERR_RPC_SYSTEM_LINK_BAD);
2437   end if;
2438 
2439   fnd_adg_object.build_all_synonyms;
2440 
2441   fnd_adg_object.compile_directive(true);
2442 
2443   l_rec := get_and_lock_adg_control;
2444 
2445   set_rpc_state(l_rec,C_RPC_SYSTEM_ENABLED);
2446 
2447   update_adg_control(l_rec);
2448 
2449 end;
2450 
2451 /*==========================================================================*/
2452 
2453 procedure switch_rpc_system_off
2454 as
2455 l_rec fnd_adg_control%rowtype;
2456 begin
2457 
2458   check_standby_support;
2459 
2460   check_rpc_state(true);
2461   check_adg_state(false);
2462 
2463   fnd_adg_object.compile_directive(false);
2464 
2465   l_rec := get_and_lock_adg_control;
2466 
2467   clr_rpc_state(l_rec,C_RPC_SYSTEM_ENABLED);
2468 
2469   update_adg_control(l_rec);
2470 
2471 end;
2472 
2473 /*==========================================================================*/
2474 
2475 procedure resync_compile_directive
2476 as
2477 l_rec fnd_adg_control%rowtype;
2478 l_compile_state boolean;
2479 l_rpc_state boolean;
2480 begin
2481 
2482 	-- If the compile directive is out of sync with RPC state,
2483 	-- then resync - source of truth is RPC state. This procedure
2484 	-- doesn't check rpc/adg state as these assume directive
2485 	-- is in sync.
2486 
2487   l_rec := get_adg_control;
2488 
2489   l_rpc_state := is_rpc_state(l_rec,C_RPC_SYSTEM_ENABLED) ;
2490 
2491   l_compile_state := compile_directive_state;
2492 
2493   if ( ( l_compile_state and l_rpc_state ) or
2494        ( not l_compile_state and not l_rpc_state ) )
2495   then
2496      null;
2497   else
2498      fnd_adg_object.compile_directive(l_rpc_state);
2499   end if;
2500 
2501 end;
2502 
2503 /*==========================================================================*/
2504 
2505 procedure compile_rpc_dependents
2506 as
2507 l_rec fnd_adg_control%rowtype;
2508 begin
2509 
2510   check_standby_support;
2511 
2512 	-- rpc state can be on or off
2513 
2514   check_adg_state(false);
2515 
2516   l_rec := get_adg_control;
2517 
2518   if ( not is_rpc_state(l_rec,C_RPC_SYSTEM_PREPARED) )
2519   then
2520      fnd_adg_exception.raise_error
2521                  (fnd_adg_exception.C_UTLERR_RPC_SYSTEM_NOT_PREPED);
2522   end if;
2523 
2524   fnd_adg_object.compile_rpc_dependents;
2525 
2526 	-- Finally one extra case - FND_ADG_SUPPORT - which has a
2527 	-- compile directive dependency.
2528 
2529   execute immediate 'alter package FND_ADG_SUPPORT compile body';
2530 
2531 end;
2532 
2533 /*==========================================================================*/
2534 
2535 function is_standby_access_supported return boolean
2536 as
2537 l_db_version varchar2(128);
2538 l_db_compat varchar2(128);
2539 l_major_version number;
2540 
2541 begin
2542 
2543   dbms_utility.db_version(l_db_version,l_db_compat);
2544 
2545   l_major_version:=to_number(substr(l_db_version,1,instr(l_db_version,'.')-1));
2546 
2547   if ( l_major_version >= 11 )
2548   then
2549      return true;
2550   else
2551      return false;
2552   end if;
2553 
2554 end;
2555 
2556 /*==========================================================================*/
2557 
2558 function is_adg_support_enabled return boolean
2559 as
2560 l_rec fnd_adg_control%rowtype;
2561 begin
2562 
2563   l_rec := get_adg_control;
2564 
2565   if ( is_rpc_state(l_rec,C_RPC_ADG_ENABLED)  and
2566        yn_to_boolean(l_rec.enable_adg_support) )
2567   then
2568      return true;
2569   else
2570      return false;
2571   end if;
2572 
2573 end;
2574 
2575 /*==========================================================================*/
2576 
2577 procedure manage_concurrent_program
2578                        (p_application_id              number,
2579                         p_concurrent_program_id       number,
2580                         p_has_run_on_primary          boolean default null,
2581                         p_has_run_on_simulated_standby boolean default null,
2582                         p_run_on_standby              boolean default null,
2583                         p_no_standby_failures         number default null,
2584                         p_max_standby_failures        number default null,
2585                         p_no_simulated_stdby_failures number default null,
2586                         p_max_simulated_stdby_failures number default null,
2587                         p_always_redirect_if_valid    boolean default null,
2588                         p_use_automatic_redirection   boolean default null
2589                        )
2590 as
2591 l_code number;
2592 begin
2593 
2594   check_standby_support;
2595 
2596 	-- ADG state can only be true when RPC is true.
2597 
2598   check_adg_state(true);
2599 
2600   set_program_access_code;
2601 
2602   l_code := G_CONC_PROGRAM_ACCESS_CODE; -- use global as get_ method
2603 				        -- resets code - i.e. use once by
2604 				        -- client.
2605 
2606   fnd_adg_support.handle_concurrent_program
2607                        (l_code,
2608                         p_application_id              ,
2609                         p_concurrent_program_id       ,
2610                         p_has_run_on_primary          ,
2611                         p_has_run_on_simulated_standby,
2612                         p_run_on_standby              ,
2613                         p_no_standby_failures         ,
2614                         p_max_standby_failures        ,
2615                         p_no_simulated_stdby_failures ,
2616                         p_max_simulated_stdby_failures,
2617                         p_always_redirect_if_valid,
2618                         p_use_automatic_redirection
2619                        );
2620 
2621 end;
2622 
2623 /*==========================================================================*/
2624 
2625 function is_runtime_validate_timestamp return boolean
2626 as
2627 l_rec fnd_adg_control%rowtype;
2628 begin
2629 
2630   l_rec := get_adg_control;
2631 
2632   if ( yn_to_boolean(l_rec.runtime_validate_timestamp) )
2633   then
2634      return true;
2635   else
2636      return false;
2637   end if;
2638 
2639 end;
2640 
2641 /*==========================================================================*/
2642 
2643 function is_always_collect_primary_data return boolean
2644 as
2645 l_rec fnd_adg_control%rowtype;
2646 begin
2647 
2648   l_rec := get_adg_control;
2649 
2650   if ( yn_to_boolean(l_rec.always_collect_primary_data) )
2651   then
2652      return true;
2653   else
2654      return false;
2655   end if;
2656 
2657 end;
2658 
2659 /*==========================================================================*/
2660 
2661 function is_enable_redirect_if_valid return boolean
2662 as
2663 l_rec fnd_adg_control%rowtype;
2664 begin
2665 
2666   l_rec := get_adg_control;
2667 
2668   if ( yn_to_boolean(l_rec.enable_redirect_if_valid) )
2669   then
2670      return true;
2671   else
2672      return false;
2673   end if;
2674 
2675 end;
2676 
2677 /*==========================================================================*/
2678 
2679 function is_standby_error_checking return boolean
2680 as
2681 l_rec fnd_adg_control%rowtype;
2682 begin
2683 
2684   l_rec := get_adg_control;
2685 
2686   if ( yn_to_boolean(l_rec.enable_standby_error_checks) )
2687   then
2688      return true;
2689   else
2690      return false;
2691   end if;
2692 
2693 end;
2694 
2695 /*==========================================================================*/
2696 
2697 function is_automatic_redirection return boolean
2698 as
2699 l_rec fnd_adg_control%rowtype;
2700 begin
2701 
2702   l_rec := get_adg_control;
2703 
2704   if ( yn_to_boolean(l_rec.enable_automatic_redirection) )
2705   then
2706      return true;
2707   else
2708      return false;
2709   end if;
2710 
2711 end;
2712 
2713 /*==========================================================================*/
2714 
2715 function get_standby_error_threshold return number
2716 as
2717 l_rec fnd_adg_control%rowtype;
2718 begin
2719 
2720   l_rec := get_adg_control;
2721 
2722   return l_rec.standby_error_threshold;
2723 
2724 end;
2725 
2726 /*==========================================================================*/
2727 
2728 function get_simulation_error_threshold return number
2729 as
2730 l_rec fnd_adg_control%rowtype;
2731 begin
2732 
2733   l_rec := get_adg_control;
2734 
2735   return l_rec.simulation_error_threshold;
2736 
2737 end;
2738 
2739 /*==========================================================================*/
2740 
2741 function is_simulated_standby_enabled return boolean
2742 as
2743 l_rec fnd_adg_control%rowtype;
2744 begin
2745 
2746   l_rec := get_adg_control;
2747 
2748   return yn_to_boolean(l_rec.enable_simulated_standby);
2749 
2750 end;
2751 
2752 /*==========================================================================*/
2753 
2754 function is_session_simulated_standby return boolean
2755 as
2756 begin
2757 
2758   if ( G_SESS_SIMULATED_STDBY_ENABLED is not null )
2759   then
2760      return G_SESS_SIMULATED_STDBY_ENABLED;
2761   end if;
2762 
2763   set_session_simulated_stdby;
2764 
2765   return G_SESS_SIMULATED_STDBY_ENABLED;
2766 
2767 end;
2768 
2769 /*==========================================================================*/
2770 
2771 function is_auto_simulation_enabled return boolean
2772 as
2773 l_rec fnd_adg_control%rowtype;
2774 begin
2775 
2776   l_rec := get_adg_control;
2777 
2778   return yn_to_boolean(l_rec.enable_auto_simulated_standby);
2779 
2780 end;
2781 
2782 /*==========================================================================*/
2783 
2784 function is_commit_wait_enabled return boolean
2785 as
2786 begin
2787 
2788   if ( G_SESS_COMMIT_WAIT_ENABLED is not null )
2789   then
2790      return G_SESS_COMMIT_WAIT_ENABLED;
2791   end if;
2792 
2793   set_commit_wait_enabled;
2794 
2795   return G_SESS_COMMIT_WAIT_ENABLED;
2796 end;
2797 
2798 /*==========================================================================*/
2799 
2800 function is_standby_manager_defined(p_req_class_app_id number,
2801                                     p_req_class_id     number,
2802                                     p_must_be_running boolean) return boolean
2803 as
2804 cursor c1 is select a.QUEUE_APPLICATION_ID,a.CONCURRENT_QUEUE_ID,
2805                     a.TYPE_APPLICATION_ID,a.type_id,
2806                     a.include_flag,a.type_code,
2807                     b.Max_Processes,b.Running_Processes
2808                from FND_CONCURRENT_QUEUE_CONTENT a,fnd_concurrent_queues b
2809               where a.QUEUE_APPLICATION_ID = b.Application_Id
2810                 and a.CONCURRENT_QUEUE_ID  = b.concurrent_queue_id
2811                 and a.type_code = 'R'
2812                 and a.include_flag = 'I'
2813                 and a.TYPE_APPLICATION_ID = p_req_class_app_id
2814                 and a.type_id             = p_req_class_id;
2815 
2816 l_defined boolean ;
2817 begin
2818 
2819   l_defined := false;
2820 
2821   for f_rec in c1 loop
2822 
2823     if ( not p_must_be_running )
2824     then
2825        l_defined := true;
2826        exit;
2827     end if;
2828 
2829     if ( f_rec.Max_Processes > 0 and f_rec.Running_Processes > 0 )
2830     then
2831        l_defined := true;
2832        exit;
2833     end if;
2834 
2835   end loop;
2836 
2837   return l_defined;
2838 
2839 end;
2840 
2841 /*==========================================================================*/
2842 
2843 procedure get_rpc_debug(p_debug_rpc out nocopy number,
2844                         p_debug_slave_rpc out nocopy number)
2845 as
2846 l_rec fnd_adg_control%rowtype;
2847 begin
2848 
2849   l_rec := get_adg_control;
2850 
2851   p_debug_rpc := l_rec.debug_rpc;
2852   p_debug_slave_rpc := l_rec.debug_slave_rpc;
2853 
2854 end;
2855 
2856 /*==========================================================================*/
2857 
2858 function get_max_commit_wait_time return number
2859 as
2860 l_rec fnd_adg_control%rowtype;
2861 begin
2862 
2863   l_rec := get_adg_control;
2864 
2865   return l_rec.max_commit_wait_time;
2866 
2867 end;
2868 
2869 /*==========================================================================*/
2870 
2871 function get_max_standby_systems return number
2872 as
2873 begin
2874 
2875   return C_MAX_STANDBY_SYSTEMS;
2876 
2877 end;
2878 
2879 /*==========================================================================*/
2880 
2881 procedure process_adg_violations(p_logoff boolean,
2882                                  p_application_id number default null,
2883                                  p_concurrent_program_id number default null)
2884 
2885 as
2886 l_cache_state boolean := null;
2887 begin
2888 
2889   l_cache_state := enable_control_cache;
2890 
2891   do_process_adg_violations(p_logoff,p_application_id,p_concurrent_program_id);
2892 
2893   disable_control_cache(l_cache_state);
2894 
2895 exception
2896   when others then
2897      disable_control_cache(l_cache_state);
2898      raise;
2899 end;
2900 
2901 /*==========================================================================*/
2902 
2903 procedure enable_violation_trace
2904 as
2905 begin
2906 
2907   execute immediate
2908           'alter session set events ''' || C_MAGIC_SWITCH_EVENT_ON || '''';
2909 
2910   G_MAGIC_SWITCH_ENABLED := true;
2911 
2912 end;
2913 
2914 /*==========================================================================*/
2915 
2916 procedure disable_violation_trace
2917 as
2918 begin
2919 
2920   execute immediate
2921           'alter session set events ''' || C_MAGIC_SWITCH_EVENT_OFF || '''';
2922 
2923   G_MAGIC_SWITCH_ENABLED := false;
2924 
2925 end;
2926 
2927 /*==========================================================================*/
2928 
2929 procedure purge_commit_wait_data
2930 as
2931 begin
2932 
2933 	-- We can purge at any time so long as this session has a valid
2934 	-- audsid.
2935 
2936   if ( user <> 'SYS' and uid <> 0 )
2937   then
2938 
2939      delete from fnd_adg_commit_wait a
2940       where a.session_id < userenv('SESSIONID')
2941         and not exists
2942             ( select 1
2943                 from gv$session b
2944                where b.audsid = a.session_id
2945             );
2946 
2947      commit;
2948 
2949   end if;
2950 
2951 end;
2952 
2953 /*==========================================================================*/
2954 
2955 function enable_control_cache return boolean
2956 as
2957 l_previous_state boolean;
2958 begin
2959 
2960   l_previous_state := G_ENABLE_CONTROL_CACHE;
2961 
2962   G_ENABLE_CONTROL_CACHE := true;
2963   G_CONTROL_CACHE_LOADED := false;
2964 
2965   return l_previous_state;
2966 
2967 end;
2968 
2969 /*==========================================================================*/
2970 
2971 procedure disable_control_cache(p_previous_state boolean default false)
2972 as
2973 begin
2974 
2975   if ( p_previous_state is not null )
2976   then
2977      G_ENABLE_CONTROL_CACHE := p_previous_state;
2978   end if;
2979 
2980 end;
2981 
2982 /*==========================================================================*/
2983 
2984 procedure refresh_control_cache
2985 as
2986 begin
2987 
2988   G_CONTROL_CACHE_LOADED := false;
2989 
2990 end;
2991 
2992 /*==========================================================================*/
2993 
2994 procedure enable_database_triggers
2995 as
2996 begin
2997 
2998   set_database_triggers(true);
2999 
3000 end;
3001 
3002 /*==========================================================================*/
3003 
3004 procedure disable_database_triggers
3005 as
3006 begin
3007 
3008   set_database_triggers(false);
3009 
3010 end;
3011 
3012 /*==========================================================================*/
3013 
3014 begin
3015   null;
3016 end fnd_adg_utility;