-
Notifications
You must be signed in to change notification settings - Fork 12
/
DBManager_IJA.py
60 lines (48 loc) · 1.52 KB
/
DBManager_IJA.py
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
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
sys.path.append("/Libs/mysql-connector-java-5.1.22-bin.jar")
from java.lang import Integer
from java.lang import Class
from java.sql import DriverManager
from java.sql import ResultSet
import threading
from threading import Lock
class DBManager(object):
GlobalConn = None
timeout = 200.0
lock = None
@staticmethod
def init():
DBManager.lock = Lock()
DBManager.GlobalConn = None
print (u'[DBManager] initialized DBManager!')
@staticmethod
def autoconnection():
# DBManager.lock.acquire()
if DBManager.GlobalConn is not None:
DBManager.GlobalConn.close()
DBManager.GlobalConn = None
Class.forName("com.mysql.jdbc.Driver").newInstance()
newConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?", "root", "")
newConn.setAutoCommit(True)
DBManager.GlobalConn = newConn
pass
@staticmethod
def requestQuery(_query):
'''
request query with lock
:param _query:
:return:
'''
# DBManager.lock.acquire()
stmt = DBManager.GlobalConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(Integer.MIN_VALUE)
items = []
try:
resultSet = stmt.executeQuery(_query)
except:
# DBManager.lock.release()
return items
# DBManager.lock.release()
return resultSet