Search in sources :

Example 11 with Transaction

use of com.cloud.utils.db.Transaction in project CloudStack-archive by CloudStack-extras.

the class TestAsyncJobManager method tstLocking.

public void tstLocking() {
    int testThreads = 20;
    Thread[] threads = new Thread[testThreads];
    for (int i = 0; i < testThreads; i++) {
        final int current = i;
        threads[i] = new Thread(new Runnable() {

            public void run() {
                final HostDao hostDao = new HostDaoImpl();
                while (true) {
                    Transaction txn = Transaction.currentTxn();
                    try {
                        HostVO host = hostDao.acquireInLockTable(getRandomLockId(), 10);
                        if (host != null) {
                            s_logger.info("Thread " + (current + 1) + " acquired lock");
                            try {
                                Thread.sleep(getRandomMilliseconds(1000, 5000));
                            } catch (InterruptedException e) {
                            }
                            s_logger.info("Thread " + (current + 1) + " released lock");
                            hostDao.releaseFromLockTable(host.getId());
                            try {
                                Thread.sleep(getRandomMilliseconds(1000, 5000));
                            } catch (InterruptedException e) {
                            }
                        } else {
                            s_logger.info("Thread " + (current + 1) + " is not able to acquire lock");
                        }
                    } finally {
                        txn.close();
                    }
                }
            }
        });
        threads[i].start();
    }
    try {
        for (int i = 0; i < testThreads; i++) threads[i].join();
    } catch (InterruptedException e) {
    }
}
Also used : Transaction(com.cloud.utils.db.Transaction) HostDaoImpl(com.cloud.host.dao.HostDaoImpl) HostDao(com.cloud.host.dao.HostDao) HostVO(com.cloud.host.HostVO)

Aggregations

Transaction (com.cloud.utils.db.Transaction)11 StackMaidDao (com.cloud.cluster.dao.StackMaidDao)3 StackMaidDaoImpl (com.cloud.cluster.dao.StackMaidDaoImpl)3 DB (com.cloud.utils.db.DB)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 SQLException (java.sql.SQLException)2 Answer (com.cloud.agent.api.Answer)1 AttachIsoCommand (com.cloud.agent.api.AttachIsoCommand)1 AttachVolumeCommand (com.cloud.agent.api.AttachVolumeCommand)1 BackupSnapshotCommand (com.cloud.agent.api.BackupSnapshotCommand)1 CheckHealthCommand (com.cloud.agent.api.CheckHealthCommand)1 CheckNetworkCommand (com.cloud.agent.api.CheckNetworkCommand)1 CleanupNetworkRulesCmd (com.cloud.agent.api.CleanupNetworkRulesCmd)1 ClusterSyncCommand (com.cloud.agent.api.ClusterSyncCommand)1 ComputeChecksumCommand (com.cloud.agent.api.ComputeChecksumCommand)1 CreatePrivateTemplateFromSnapshotCommand (com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand)1 CreatePrivateTemplateFromVolumeCommand (com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand)1 CreateStoragePoolCommand (com.cloud.agent.api.CreateStoragePoolCommand)1 CreateVolumeFromSnapshotCommand (com.cloud.agent.api.CreateVolumeFromSnapshotCommand)1 DeleteSnapshotBackupCommand (com.cloud.agent.api.DeleteSnapshotBackupCommand)1