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.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Date;
27 import java.util.List;
28 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
29 import org.onap.ccsdk.sli.adaptors.util.db.CachedDataSourceWrap;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.jdbc.core.JdbcTemplate;
33 import org.springframework.jdbc.core.RowMapper;
35 public class ResourceLockDaoImpl implements ResourceLockDao {
37 @SuppressWarnings("unused")
38 private static final Logger log = LoggerFactory.getLogger(ResourceLockDaoImpl.class);
40 private JdbcTemplate jdbcTemplate;
43 public void add(ResourceLock l) {
45 "INSERT INTO RESOURCE_LOCK (resource_name, lock_holder, lock_count, lock_time, expiration_time)\n" +
46 "VALUES (?, ?, ?, ?, ?)",
47 new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime });
51 public void update(long id, Date lockTime, Date expirationTime, int lockCount) {
53 "UPDATE RESOURCE_LOCK SET lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?",
54 new Object[] { lockTime, expirationTime, lockCount, id });
58 public ResourceLock getByResourceName(String resourceName) {
59 List<ResourceLock> ll = jdbcTemplate.query("SELECT * FROM RESOURCE_LOCK WHERE resource_name = ?",
60 new Object[] { resourceName }, new RowMapper<ResourceLock>() {
63 public ResourceLock mapRow(ResultSet rs, int rowNum) throws SQLException {
64 ResourceLock rl = new ResourceLock();
65 rl.id = rs.getLong("resource_lock_id");
66 rl.resourceName = rs.getString("resource_name");
67 rl.lockHolder = rs.getString("lock_holder");
68 rl.lockCount = rs.getInt("lock_count");
69 rl.lockTime = rs.getTimestamp("lock_time");
70 rl.expirationTime = rs.getTimestamp("expiration_time");
74 return ll != null && !ll.isEmpty() ? ll.get(0) : null;
78 public void delete(long id) {
79 jdbcTemplate.update("DELETE FROM RESOURCE_LOCK WHERE resource_lock_id = ?", new Object[] { id });
83 public void decrementLockCount(long id) {
84 jdbcTemplate.update("UPDATE RESOURCE_LOCK SET lock_count = lock_count - 1 WHERE resource_lock_id = ?",
88 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
89 this.jdbcTemplate = jdbcTemplate;
93 public void commit() {
94 if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
95 CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
97 ds.releaseConnection();
102 public void rollback() {
103 if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
104 CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
106 ds.releaseConnection();