Python:由于不同的数据库连接,SELECT查询未获取更新的结果集

时间:2018-08-19 12:28:09

标签: mysql python-3.x pymysql amazon-rds-aurora

因此,我正在研究一个脚本,该脚本与Master进行写操作,并与slave进行读取操作。在我的机器上,我创建了两个连接方法,如下所示:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
    _id: Schema.Types.ObjectId,
    salt: String,
    provider: String,
    name: String,
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    _subscriptions: [{
        field: {
            type: mongoose.Schema.ObjectId,
            ref: 'Field',
        },
        status: String,
        dateSubscribed: Date,
        payments: [{}]
    }],
    role: String,
});

module.exports = mongoose.model('User', UserSchema);

现在,尽管在循环中运行var Field = require('../model/Field'); var express = require('express'); var router = express.Router(); var User = require('../model/User'); router.get('/',function(req, res, next) { User.find({}, function(err, result) { if (err) { console.log(err); res.send('something wrong'); } res.status(200).send(result); }).populate( '_subscriptions.field').exec(function (err, story) { if (err) return handleError(err); console.log('Here!!!!!'); }); }); router.get('/findById/:id',function(req, res, next) { var id = req.params.id; User.findById(id, function(err, doc) { if (err) { console.error('error, no entry found'); } res.status(200).send(doc); }).populate('field').exec(function (err, story) { if (err) return handleError(err); console.log('Here!!!!!'); }); }); router.get('/getSubscriptions/:id',function(req, res, next) { var id = req.params.id; User.findById(id, function(err, doc) { if (err) { console.error('error, no entry found'); } var type = typeof(doc); res.status(200).send(doc); }) }); module.exports = router; def get_read_connection(): connection = None read_host = 'localhost' try: if sys.platform != 'darwin': read_host = 'remote_rds_host.com' read_c = pymysql.connect( host=read_host, user=user, password=password, db=db_name, autocommit=False, cursorclass=pymysql.cursors.DictCursor) print('Read Connected') except Exception as ex: print('Exception in Read Connection') print(str(ex)) finally: return read_c def get_connection(): connection = None try: connection = pymysql.connect(host=host, user=user, password=password, db=db_name, cursorclass=pymysql.cursors.DictCursor) print('Connected') except Exception as ex: print(str(ex)) finally: return connection 会获取先前的记录状态,但我遇到了一个奇怪的问题。执行以下操作的功能;

connection.commit()

SELECT中,我正在这样做:

def get_links(size=4):
    total_links = []
    sql = None

    try:
        if connection is not None:
            # connection_read.ping(reconnect=True)
            now = datetime.datetime.now()
            # print("Current date and time using strftime:")
            # print(now.strftime("%Y-%m-%d %H:%M"))
            with connection.cursor() as cursor:
                # sql = ' SELECT * from zillow_links_master where status = 0 LIMIT ' + str(size)
                sql = " select license_id  from " + TABLE_NAME + " WHERE status = 0 ORDER BY id  LIMIT " + str(size)
                print(sql)
                cursor.execute(sql)
                links = cursor.fetchall()

                for link in links:
                    total_links.append(link['license_id'])

            print(tuple(total_links))
            print('======================================')
            if len(total_links) > 0:
                if connection is not None:
                    now = datetime.datetime.now()
                    # print("Current date and time using strftime:")
                    # print(now.strftime("%Y-%m-%d %H:%M"))
                    with connection.cursor() as cursor:
                        format_strings = ','.join(['%s'] * len(total_links))
                        # sql = " UPDATE " + TABLE_NAME + " set is_processed = 4 WHERE url IN (%s)" % format_strings,
                        sql = " UPDATE " + TABLE_NAME + " set status = 1 WHERE license_id IN ({})".format(
                            format_strings)
                        cursor.execute(sql, tuple(total_links))
                        connection.commit()
                        print(cursor.rowcount)
    except Exception as ex:
        print('Exception in get_links')
        crash_date = time.strftime("%Y-%m-%d %H:%m:%S")
        crash_string = "".join(traceback.format_exception(etype=type(ex), value=ex, tb=ex.__traceback__))
        exception_string = '[' + crash_date + '] - ' + crash_string + '\n'
        print(exception_string)
        print(sql)
        if 'lost' in exception_string:
            print('Connection lost')
            # connection_read.ping()
    finally:
        return total_links

如您所见,我正在使用commit,它确实在第一次生效,但是SELECT没有返回唯一记录。我遇到的问题是,__main__仅在我使用其他读/写连接时才第一次返回新记录。否则,它只会一次又一次地返回while True: print('Block Begin') now = datetime.datetime.now() # print("Current date and time using strftime:") # print(now.strftime("%Y-%m-%d %H:%M")) p_links = get_links(LIMIT) for p in p_links: l_id = p result = parse(l_id) store_lic_info(l_id, result) sleep(2) connection.commit() print('Block End') sleep(2) 中的相同记录。

我在做什么错?我一点都不知道。在本地和远程Amazon Aurora RDS上的行为相同。

0 个答案:

没有答案