2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.lock.dao;
24 import java.util.Date;
25 import java.util.List;
26 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
27 import org.onap.ccsdk.sli.adaptors.util.db.CachedDataSourceWrap;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.jdbc.core.JdbcTemplate;
31 import org.springframework.jdbc.core.RowMapper;
33 public class ResourceLockDaoImpl implements ResourceLockDao {
35 @SuppressWarnings("unused")
36 private static final Logger log = LoggerFactory.getLogger(ResourceLockDaoImpl.class);
38 private JdbcTemplate jdbcTemplate;
41 public void add(ResourceLock l) {
43 "INSERT INTO RESOURCE_LOCK (resource_name, lock_holder, lock_count, lock_time, expiration_time)\n" +
44 "VALUES (?, ?, ?, ?, ?)",
45 new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime });
49 public void update(long id, String lockHolder, Date lockTime, Date expirationTime, int lockCount) {
51 "UPDATE RESOURCE_LOCK SET lock_holder = ?, lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?",
52 new Object[] { lockHolder, lockTime, expirationTime, lockCount, id });
56 public ResourceLock getByResourceName(String resourceName) {
57 List<ResourceLock> ll = jdbcTemplate.query("SELECT * FROM RESOURCE_LOCK WHERE resource_name = ?",
58 new Object[] { resourceName }, (RowMapper<ResourceLock>) (rs, rowNum) -> {
59 ResourceLock rl = new ResourceLock();
60 rl.id = rs.getLong("resource_lock_id");
61 rl.resourceName = rs.getString("resource_name");
62 rl.lockHolder = rs.getString("lock_holder");
63 rl.lockCount = rs.getInt("lock_count");
64 rl.lockTime = rs.getTimestamp("lock_time");
65 rl.expirationTime = rs.getTimestamp("expiration_time");
68 return ll != null && !ll.isEmpty() ? ll.get(0) : null;
72 public void delete(long id) {
73 jdbcTemplate.update("DELETE FROM RESOURCE_LOCK WHERE resource_lock_id = ?", new Object[] { id });
77 public void decrementLockCount(long id) {
78 jdbcTemplate.update("UPDATE RESOURCE_LOCK SET lock_count = lock_count - 1 WHERE resource_lock_id = ?",
82 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
83 this.jdbcTemplate = jdbcTemplate;
87 public void commit() {
88 if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
89 CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
91 ds.releaseConnection();
96 public void rollback() {
97 if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
98 CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
100 ds.releaseConnection();