python cx_Oracle 自动重连

# !/usr/bin/env python
# -*- coding: utf-8 -*-
import cx_Oracle
from pprint import pprint
import csv
import time
import re
import binascii
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
print time.ctime()
conn = cx_Oracle.connect('test/test@10.10.17.200/serv')
print '--------conn-------------'
print conn
print type(conn)
print '--------conn-------------'
cursor = conn.cursor()
print '--------cursor------------'
print cursor
print type(cursor)
print '--------cursor------------'
def get_sql():
 xsql="select 'aaa' from dual"
 cursor.execute(xsql)
 result = cursor.fetchall()
 print result
def check_db():
    ysql='select 0 from dual'
    try:
        cursor.execute(ysql)
        return 0
    except Exception,e:
        print e
        return 1
while True:
  a=check_db()

  if a==0:
      print a
      get_sql()
      time.sleep(5)
  else:
      print a
      try:
       conn = cx_Oracle.connect('test/test@10.10.17.200/serv')
       cursor = conn.cursor()
       get_sql()
       time.sleep(5)
      except Exception,e:
        print e
        time.sleep(5)
原文地址:https://www.cnblogs.com/hzcya1995/p/13349017.html