18
18
from typing import Any , List , Optional , Tuple , Type , TypedDict , Union
19
19
20
20
import system .db
21
+ import system .util
21
22
from com .inductiveautomation .ignition .common import BasicDataset
22
23
from java .lang import Thread
23
24
@@ -42,9 +43,6 @@ class DisposableConnection(object):
42
43
resources.
43
44
"""
44
45
45
- database = None # type: AnyStr
46
- retries = None # type: int
47
-
48
46
def __init__ (self , database , retries = 3 ):
49
47
# type: (AnyStr, int) -> None
50
48
"""Disposable Connection initializer.
@@ -56,14 +54,21 @@ def __init__(self, database, retries=3):
56
54
enabling the connection. Optional.
57
55
"""
58
56
super (DisposableConnection , self ).__init__ ()
59
- self .database = database
60
- self .retries = retries
57
+ self ._database = database
58
+ self ._retries = retries
59
+ self ._global_conn = "incendium_db_{}" .format (database )
60
+
61
+ @property
62
+ def database (self ):
63
+ # type: () -> AnyStr
64
+ """Get the name of the disposable connection."""
65
+ return self ._database
61
66
62
67
@property
63
68
def status (self ):
64
69
# type: () -> AnyStr
65
70
"""Get connection status."""
66
- connection_info = system .db .getConnectionInfo (self .database )
71
+ connection_info = system .db .getConnectionInfo (self ._database )
67
72
return str (connection_info .getValueAt (0 , "Status" ))
68
73
69
74
def __enter__ (self ):
@@ -74,22 +79,25 @@ def __enter__(self):
74
79
IOError: If the connection's status reports as Faulted, or
75
80
ir cannot be enabled.
76
81
"""
77
- system .db .setDatasourceEnabled (self .database , True )
82
+ system .db .setDatasourceEnabled (self ._database , True )
78
83
79
- for _ in range (self .retries ):
84
+ for _ in range (self ._retries ):
80
85
Thread .sleep (1000 )
81
86
if self .status == "Valid" :
87
+ if self ._global_conn not in system .util .globals :
88
+ system .util .globals [self ._global_conn ] = 0
89
+ system .util .globals [self ._global_conn ] += 1
82
90
break
83
91
if self .status == "Faulted" :
84
92
raise IOError (
85
93
"The database connection {!r} is {}." .format (
86
- self .database , self .status
94
+ self ._database , self .status
87
95
)
88
96
)
89
97
else :
90
98
raise IOError (
91
99
"The database connection {!r} could not be enabled." .format (
92
- self .database
100
+ self ._database
93
101
)
94
102
)
95
103
return self
@@ -102,7 +110,9 @@ def __exit__(
102
110
):
103
111
# type: (...) -> None
104
112
"""Exit the runtime context related to this object."""
105
- system .db .setDatasourceEnabled (self .database , False )
113
+ system .util .globals [self ._global_conn ] -= 1
114
+ if system .util .globals [self ._global_conn ] == 0 :
115
+ system .db .setDatasourceEnabled (self ._database , False )
106
116
107
117
108
118
class Param (object ):
0 commit comments