Quartz (三) 锁机制

前言:一个Quartz集群中的每个节点是一个独立的Quartz应用,它又管理着其他的节点。这就意味着你必须对每个节点分别启动或停止。Quartz集群中,独立的Quartz节点并不与另一其的节点或是管理节点通信,而是通过相同的数据库表来感知到另一Quartz应用的 

quartz 通过数据库的行级锁,来实现分布式锁(一种悲观锁)

通过这个机制,集群中一次只能有一个线程来操作 加锁 -  操作 - 释放锁。来保证多个结点的应用只  如果操作的时间过长的话,会带来集群间的主线程等待。
数据库行锁是一种悲观锁,锁表时其它线程无法查询。

Semaphore 接口

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 */
package org.quartz.impl.jdbcjobstore;

import java.sql.Connection;

/**
 * An interface for providing thread/resource locking in order to protect
 * resources from being altered by multiple threads at the same time.
 * 
 * @author jhouse
 */
public interface Semaphore {

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * 
     * Interface.
     * 
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    /**
     * Grants a lock on the identified resource to the calling thread (blocking
     * until it is available).
     * 
     * @param conn Database connection used to establish lock.  Can be null if
     * <code>{@link #requiresConnection()}</code> returns false.
     * 
     * @return true if the lock was obtained.
     */
    boolean obtainLock(Connection conn, String lockName) throws LockException;

    /**
     * Release the lock on the identified resource if it is held by the calling
     * thread.
     */
    void releaseLock(String lockName) throws LockException;

    /**
     * Whether this Semaphore implementation requires a database connection for
     * its lock management operations.
     * 
     * @see #obtainLock(Connection, String)
     * @see #releaseLock(String)
     */
    boolean requiresConnection();
}

StdRowLockSemaphore.java

public static final String SELECT_FOR_LOCK = "SELECT * FROM "
            + TABLE_PREFIX_SUBST + TABLE_LOCKS + " WHERE " + COL_SCHEDULER_NAME + " = " + SCHED_NAME_SUBST
            + " AND " + COL_LOCK_NAME + " = ? FOR UPDATE";

    public static final String INSERT_LOCK = "INSERT INTO "
        + TABLE_PREFIX_SUBST + TABLE_LOCKS + "(" + COL_SCHEDULER_NAME + ", " + COL_LOCK_NAME + ") VALUES (" 
        + SCHED_NAME_SUBST + ", ?)"; 

使用 select  ... for update 对数据库表进row进行加锁

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Quartz中,数据库集群是一种高可用性的解决方案。它允许多个Quartz实例共享同一个数据库,并且能够自动协调任务的执行,从而保证任务的可靠性和稳定性。 但是,在多个Quartz实例同时操作同一个数据库时,必须确保它们之间的数据一致性。这就需要使用锁机制来保证数据的正确性和安全性。 Quartz提供了两种类型的锁:悲观锁和乐观锁。它们分别采用不同的方式来保证数据的一致性。 1. 悲观锁 悲观锁是一种悲观的认为并发环境下会出现冲突的锁机制。它在操作数据时,会先加锁,然后再进行操作,操作完成后再释放锁。 在Quartz中,悲观锁是通过数据库中的行级锁来实现的。当一个Quartz实例要对某个任务进行操作时,它会先获取该任务的行级锁,然后再进行操作。其他实例在此期间无法获取该任务的锁,从而保证了数据的一致性。 2. 乐观锁 乐观锁是一种乐观的认为并发环境下不会出现冲突的锁机制。它在操作数据时,不会加锁,而是通过版本号等方式来判断数据是否发生了变化。 在Quartz中,乐观锁是通过版本号来实现的。每个任务都有一个版本号,当一个Quartz实例要对某个任务进行操作时,它会先获取该任务的版本号,然后进行操作。如果在此期间该任务的版本号发生了变化,则说明其他实例已经对该任务进行了操作,当前实例的操作会失败,需要重新获取版本号并重试。这样可以保证数据的一致性。 总的来说,悲观锁适用于高并发、数据冲突严重的场景,但是会带来较大的性能开销;而乐观锁适用于并发量较小、数据冲突不严重的场景,性能开销较小。在Quartz中,可以根据具体的业务需求选择合适的锁机制来保证数据的正确性和安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值