Skip to main content

transactional management using sqlalchemy

Project description

Transactional-SQLAlchemy

개요

지원하는 트랜잭션 전파 방식

참조: Transaction Propagation of Spring framework

  • REQUIRED : 이미 트랜잭션이 열린경우 기존의 세션을 사용하거나, 새로운 트랜잭션을 생성
  • REQUIRES_NEW : 기존 트랜잭션을 무시하고 새롭게 생성
  • NESTED : 기존 트랜잭션의 자식 트랜잭션을 생성

기능

트랜잭션 전파 방식 관리

Auto commit or Rollback (트랜잭션 사용 시)

auto session

동기/비동기 함수 모두 지원

사용법

1. transactional + auto session

  1. 패키지 설치
  • ver. sync
pip install transactional-sqlalchemy
  • ver. async
pip install transactional-sqlalchemy[async]
  1. 세션 핸들러 초기화
from transactional_sqlalchemy import init_manager
from sqlalchemy.ext.asyncio import async_scoped_session

async_scoped_session_ = async_scoped_session(
    async_session_factory, scopefunc=asyncio.current_task
)

init_manager(async_scoped_session_)
  1. ITransactionalRepository를 상속하는 클래스 작성
  • repository 레이어의 클래스 작성시, ITransactionalRepository를 상속
  • session이라는 이름의 변수가 있는경우 만들어 두었던 세션을 할당
from transactional_sqlalchemy import ITransactionalRepository, transactional

class PostRepository(ITransactionalRepository):
    @transactional # or @transactional(propagation=Propagation.REQUIRES)
    async def requires(self, post: Post, session: AsyncSession) -> None:
        session.add(post)
        ...

    @transactional(propagation=Propagation.REQUIRES_NEW)
    async def requires_new(self, post: Post, session: AsyncSession) -> None: ...

    @transactional(propagation=Propagation.NESTED)
    async def nested(self, post: Post, session: AsyncSession) -> None: ...

    async def auto_session_allocate(self, session:AsyncSession) -> None: ...

2. auto session without transactional

from transactional_sqlalchemy import ISessionRepository


class PostRepository(ISessionRepository):

    async def create(self, post: Post, *, session: AsyncSession = None) -> None: ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

transactional_sqlalchemy-0.1.2.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

transactional_sqlalchemy-0.1.2-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file transactional_sqlalchemy-0.1.2.tar.gz.

File metadata

File hashes

Hashes for transactional_sqlalchemy-0.1.2.tar.gz
Algorithm Hash digest
SHA256 48549d495200b60e52104a02135b96d6845cf8cf1afaf989c5f4145239a1bc91
MD5 b1d715bcfb4154dfab17b3e71b03d9c4
BLAKE2b-256 ea7aed07f2e96eb15195c2674cdf0f441aa3963799000a6a872f5aa648e1027b

See more details on using hashes here.

File details

Details for the file transactional_sqlalchemy-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for transactional_sqlalchemy-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c565d984d4b23bbba577277329fc84ef8b3fa360c7a4127ff3971ea01c552ad
MD5 bde649070c28740524642751fe4d162a
BLAKE2b-256 01d4d9846f37d0a91be38219365c5dc4dca668fcae0b8761dbd4e4cd965b4b88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page