"Python 오라클 연결 cx oracle"의 두 판 사이의 차이
DB CAFE
(→접속 테스트) |
(→커넥션 풀 사용하기) |
||
77번째 줄: | 77번째 줄: | ||
</source> | </source> | ||
+ | === DRCP 커넥션풀 사용하기 === | ||
+ | |||
+ | {{고지상자 | ||
+ | |제목 = DRCP (Database Resident Connection Pooling) | ||
+ | |내용 = # DRCP (Database Resident Connection Pooling) 기능은 DB 접속을 위한 커넥션 공유풀을 공동사용하게 함으로써 DB 서버의 자원 사용을 절약하는 기능 | ||
+ | # 만약 1000개의 프로세스가 DB 접속 하는 경우, 기본적으로 1000개의 클라이언트 처리요청을 지원하기 위해 1000개의 Dedicated Server Process 를 기동 해야함 | ||
+ | # WAS 를 사용하지 않는 경우, DB 차원에서 이런 "공유 커넥션 풀" 기능을 제공 | ||
+ | # 오라클 DB에서 2가지 Shared Server 방식과 DRCP(Database Resident Connection Pooling) 방식 제공 | ||
+ | }} | ||
+ | |||
+ | * 동시 접속이 많은 경우 DB 서버의 자원이 금방 고갈될 수 밖에 없는 문제를 이런 것을 해결하기 위해 보통은 WAS (Web Application Server) 차원에서 커넥션풀을 만들어서 DB접속풀 공유하면서 사용하는 것이 일반적임. | ||
+ | <source lang=python> | ||
+ | |||
+ | import cx_Oracle | ||
+ | import threading | ||
+ | |||
+ | pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled", | ||
+ | min = 2, max = 5, increment = 1, threaded = True, | ||
+ | getmode = cx_Oracle.SPOOL_ATTRVAL_WAIT) | ||
+ | |||
+ | def Query(): | ||
+ | con = pool.acquire(cclass = "PYTHONHOL", purity = cx_Oracle.ATTR_PURITY_SELF) | ||
+ | cur = conn.cursor() | ||
+ | for i in range(4): | ||
+ | cur.execute("select myseq.nextval from dual") | ||
+ | seqval, = cur.fetchone() | ||
+ | print("Thread", threading.current_thread().name, "fetched sequence =", seqval) | ||
+ | |||
+ | numberOfThreads = 2 | ||
+ | threadArray = [] | ||
+ | |||
+ | for i in range(numberOfThreads): | ||
+ | thread = threading.Thread(name = '#' + str(i), target = Query) | ||
+ | threadArray.append(thread) | ||
+ | thread.start() | ||
+ | |||
+ | for t in threadArray: | ||
+ | t.join() | ||
+ | |||
+ | print("All done!") | ||
+ | </source> | ||
[[Category:python]] | [[Category:python]] |
2023년 5월 31일 (수) 18:41 판
thumb_up 추천메뉴 바로가기
- DBA { Oracle DBA 명령어 > DBA 초급 과정 > DBA 고급 과정 }
- 튜닝 { 오라클 튜닝 목록 }
- 모델링 { 데이터 모델링 가이드 }
1 개요[편집]
2 접속 모듈 설치[편집]
pip install cx_Oracle
2.1 사용자 매뉴얼[편집]
https://cx-oracle.readthedocs.io/en/latest/user_guide/sql_execution.html#fetch-methods
2.2 접속 테스트[편집]
import cx_Oracle
#한글 지원 방법
import os
os.putenv('NLS_LANG', '.UTF8')
#연결에 필요한 기본 정보 (유저, 비밀번호, 데이터베이스 서버 주소)
connection = cx_Oracle.connect('Id','password','localhost/orcl')
# 오라클 버전 확인
print("Database version:", connection.version)
print("Client version:", cx_Oracle.clientversion())
# 커셔 연결
cursor = connection.cursor()
cursor.execute("""
select name
from test_db
where text = :texting""",
texting = "테스트"
)
for name in cursor:
print("테스트 이름 리스트 : ", name)
# 종료
cursor.close()
connection.close()
2.3 커넥션 풀 사용하기[편집]
import cx_Oracle
import threading
import db_config
pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn,
min = 2, max = 5, increment = 1, threaded = True,
getmode = cx_Oracle.SPOOL_ATTRVAL_WAIT)
def Query():
con = pool.acquire()
cur = con.cursor()
for i in range(4):
cur.execute("select myseq.nextval from dual")
seqval, = cur.fetchone()
print("Thread", threading.current_thread().name, "fetched sequence =", seqval)
numberOfThreads = 2
threadArray = []
for i in range(numberOfThreads):
thread = threading.Thread(name = '#' + str(i), target = Query)
threadArray.append(thread)
thread.start()
for t in threadArray:
t.join()
print("All done!")
2.4 DRCP 커넥션풀 사용하기[편집]
- 동시 접속이 많은 경우 DB 서버의 자원이 금방 고갈될 수 밖에 없는 문제를 이런 것을 해결하기 위해 보통은 WAS (Web Application Server) 차원에서 커넥션풀을 만들어서 DB접속풀 공유하면서 사용하는 것이 일반적임.
import cx_Oracle
import threading
pool = cx_Oracle.SessionPool(db_config.user, db_config.pw, db_config.dsn + ":pooled",
min = 2, max = 5, increment = 1, threaded = True,
getmode = cx_Oracle.SPOOL_ATTRVAL_WAIT)
def Query():
con = pool.acquire(cclass = "PYTHONHOL", purity = cx_Oracle.ATTR_PURITY_SELF)
cur = conn.cursor()
for i in range(4):
cur.execute("select myseq.nextval from dual")
seqval, = cur.fetchone()
print("Thread", threading.current_thread().name, "fetched sequence =", seqval)
numberOfThreads = 2
threadArray = []
for i in range(numberOfThreads):
thread = threading.Thread(name = '#' + str(i), target = Query)
threadArray.append(thread)
thread.start()
for t in threadArray:
t.join()
print("All done!")