Skip to main content

Python implementation of distributed transaction processing with MySQL, MySQLdb

Project description

Python分布式事务实现:

  1. TX specification
  2. XA specification

运行环境:python2.7,所支持的db为:

  1. mysql(>=5.7) + MySQLdb(>=1.2.3)
  2. mysql(>=5.7) + mysql-connector(>=2.1.0)
  3. postgresql(>=8.4) + psycopg2

安装: 获取源码,并进入源码目录

# python setup.py install

用法:(暂不支持用unix_socket连接mysql)

import MySQLdb
from python_dtp import local_db, MySQLResourceManager, PostgreSQLResourceManager, TransactionManager

def test():
    conn1, conn2 = MySQLdb.connect(**db1_conf), MySQLdb.connect(**db2_conf)
    # conn1, conn2也可以是mysql.connector.connection对象,或psycopg2.connection对象

    rm1, rm2 = MySQLResourceManager(conn1), MySQLResourceManager(conn2)
    cursor1, cursor2 = conn1.cursor(), conn2.cursor()

    local_db.path = '/path/to/dtp.sqlite3' # 很重要,记录执行失败的事务信息,crash恢复

    tm = TransactionManager(rm1, rm2)
    tm.tx_begin()
    cursor1.execute('insert into test1 values(1, "bbb")', ())
    cursor2.execute('insert into test2 values(NULL, "aaa")', ())
    tm.tx_commit()

在同一台机器上,跑如下脚本,用来crash恢复:

from python_dtp import monitor, local_db

local_db.path = '/path/to/dtp.sqlite3' # 和上面的local_db.path一致

# 定义db的连接参数
db1_conf = {'host':'xxx', 'port':3306, 'user':'xxx', 'passwd':'xxx', 'db':'xxx'} # for mysql, 必须是MySQLdb能识别的参数
db2_conf = {'host':'xxx', 'port':3306, 'user':'xxx', 'password':'xxx', 'dbname':'xxx'} # for postgresql

# 告诉monitor,如何去连接db,其中key的值为形如(host, port)的tuple
monitor.db_config = {
    ('127.0.0.1', 60000) : db1_conf,
    ('localhost', 3307) : db2_conf,
    ('123.38.76.10', 3306) : db3_conf,
    ('test_db.nease.net', 3306) : db4_conf,
}


monitor.run() # 启动monitor,一直运行

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

python_dtp-0.9.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

python_dtp-0.9.3-py2-none-any.whl (11.0 kB view details)

Uploaded Python 2

File details

Details for the file python_dtp-0.9.3.tar.gz.

File metadata

  • Download URL: python_dtp-0.9.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/2.7.13

File hashes

Hashes for python_dtp-0.9.3.tar.gz
Algorithm Hash digest
SHA256 917ecbc4fb32918029bd31eca5fc1600d2fe0794ebb431d18147df74e3eec330
MD5 495647a6960377ec1fd219fc74971038
BLAKE2b-256 aca08961cf6e2714dcd8ef0a3bc041474e028440c200055d8b4757ac84bf052e

See more details on using hashes here.

File details

Details for the file python_dtp-0.9.3-py2-none-any.whl.

File metadata

  • Download URL: python_dtp-0.9.3-py2-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.13.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/2.7.13

File hashes

Hashes for python_dtp-0.9.3-py2-none-any.whl
Algorithm Hash digest
SHA256 46f0fe5a40f630b27ac2dba23fb45db96dec27995d9fae078939839b8138afcd
MD5 b76db352fecf2f398a3b5c86a4f20640
BLAKE2b-256 872c4e5db60d6be2dc2539a2e787c82baf20fceda47bf943e47e2c9b5e2baf72

See more details on using hashes here.

Supported by

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