forked from godror/godror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_shutdown_test.go
100 lines (90 loc) · 2.48 KB
/
example_shutdown_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018, 2020 The Godror Authors
//
//
// SPDX-License-Identifier: UPL-1.0 OR Apache-2.0
package godror_test
import (
"context"
"database/sql"
"fmt"
"log"
godror "github.com/godror/godror"
)
// ExampleStartupMode calls exampleStartup to start a database.
func ExampleStartupMode() {
if err := exampleStartup(godror.StartupDefault); err != nil {
log.Fatal(err)
}
}
func exampleStartup(startupMode godror.StartupMode) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dsn := "oracle://?sysdba=1&prelim=1"
db, err := sql.Open("godror", dsn)
if err != nil {
log.Fatal(fmt.Errorf("%s: %w", dsn, err))
}
defer db.Close()
err = godror.Raw(ctx, db, func(oraDB godror.Conn) error {
log.Println("Starting database")
if err = oraDB.Startup(startupMode); err != nil {
return err
}
return nil
})
// You cannot alter database on the prelim_auth connection.
// So open a new connection and complete startup, as Startup starts pmon.
db2, err := sql.Open("godror", "oracle://?sysdba=1")
if err != nil {
return err
}
defer db2.Close()
log.Println("Mounting database")
if _, err = db2.Exec("alter database mount"); err != nil {
return err
}
log.Println("Opening database")
if _, err = db2.Exec("alter database open"); err != nil {
return err
}
return nil
}
// ExampleShutdownMode is an example of how to shut down a database.
func ExampleShutdownMode() {
dsn := "oracle://?sysdba=1" // equivalent to "/ as sysdba"
db, err := sql.Open("godror", dsn)
if err != nil {
log.Fatal(fmt.Errorf("%s: %w", dsn, err))
}
defer db.Close()
if err = exampleShutdown(db, godror.ShutdownTransactionalLocal); err != nil {
log.Fatal(err)
}
}
func exampleShutdown(db *sql.DB, shutdownMode godror.ShutdownMode) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := godror.Raw(ctx, db, func(oraDB godror.Conn) error {
log.Printf("Beginning shutdown %v", shutdownMode)
return oraDB.Shutdown(shutdownMode)
})
if err != nil {
return err
}
// If we abort the shutdown process is over immediately.
if shutdownMode == godror.ShutdownAbort {
return nil
}
log.Println("Closing database")
if _, err = db.Exec("alter database close normal"); err != nil {
return err
}
log.Println("Unmounting database")
if _, err = db.Exec("alter database dismount"); err != nil {
return err
}
log.Println("Finishing shutdown")
return godror.Raw(ctx, db, func(oraDB godror.Conn) error {
return oraDB.Shutdown(godror.ShutdownFinal)
})
}