diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java index 7e571fa49ff..fb7bb279e0d 100755 --- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java +++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java @@ -18,8 +18,6 @@ package com.cloud.storage.snapshot; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -111,8 +109,6 @@ import com.cloud.vm.dao.UserVmDao; @Local(value={SnapshotManager.class, SnapshotService.class}) public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Manager { private static final Logger s_logger = Logger.getLogger(SnapshotManagerImpl.class); - private static final String GET_LAST_ID = "SELECT id FROM cloud.snapshots ORDER BY id DESC LIMIT 1"; - private static final String UPDATE_SNAPSHOT_SEQ = "UPDATE cloud.sequence SET value=? WHERE name='snapshots_seq'"; @Inject protected HostDao _hostDao; @Inject protected UserVmDao _vmDao; @@ -1150,40 +1146,6 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma return _snapshotDao.getNextInSequence(Long.class, "id"); } - private Long _getLastId() { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - PreparedStatement pstmt = null; - String sql = GET_LAST_ID; - try { - pstmt = txn.prepareAutoCloseStatement(sql); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { - return Long.valueOf(rs.getLong(1)); - } - } catch (Exception ex) { - s_logger.error("error getting last id", ex); - } - return 1l; - } - - private void _updateSnapshotSeq(Long seq) { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - txn.start(); - String sql = UPDATE_SNAPSHOT_SEQ; - PreparedStatement pstmt = null; - pstmt = txn.prepareAutoCloseStatement(sql); - pstmt.setLong(1, seq.longValue()); - pstmt.execute(); - txn.commit(); - } catch (Exception ex) { - txn.rollback(); - String msg = "error seting snapshots_seq to " + seq; - s_logger.error(msg, ex); - throw new CloudRuntimeException(msg, ex); - } - } - @Override public boolean configure(String name, Map params) throws ConfigurationException { _name = name; @@ -1203,14 +1165,6 @@ public class SnapshotManagerImpl implements SnapshotManager, SnapshotService, Ma _totalRetries = NumbersUtil.parseInt(configDao.getValue("total.retries"), 4); _pauseInterval = 2*NumbersUtil.parseInt(configDao.getValue("ping.interval"), 60); - Long lastId = _getLastId(); - if ( lastId == null ) { - String msg = "Can not get last id of snapshots"; - s_logger.error(msg); - throw new CloudRuntimeException(msg); - } - s_logger.info("Set shapshot sequence to " + (lastId + 1)); - _updateSnapshotSeq( lastId + 1 ); s_logger.info("Snapshot Manager is configured."); return true; diff --git a/setup/db/data-21to22.sql b/setup/db/data-21to22.sql index 7fcc35e36a8..5a7f82136be 100644 --- a/setup/db/data-21to22.sql +++ b/setup/db/data-21to22.sql @@ -17,5 +17,6 @@ INSERT INTO vm_template (id, unique_name, name, public, created, type, hvm, bits Update configuration set name='storage.max.volume.size' where name='max.volume.size.mb'; INSERT INTO sequence (name, value) VALUES ('snapshots_seq', '1') +UPDATE cloud.sequence SET value=IF((SELECT COUNT(*) FROM cloud.snapshots) > 0, (SELECT max(id) FROM cloud.snapshots) + 1, 1) WHERE name='snapshots_seq' COMMIT; diff --git a/setup/db/data-22beta1to22beta2.sql b/setup/db/data-22beta1to22beta2.sql index f52a94b3651..1d082a6fd8d 100644 --- a/setup/db/data-22beta1to22beta2.sql +++ b/setup/db/data-22beta1to22beta2.sql @@ -1,2 +1,3 @@ INSERT INTO sequence (name, value) VALUES ('snapshots_seq', '1') +UPDATE cloud.sequence SET value=IF((SELECT COUNT(*) FROM cloud.snapshots) > 0, (SELECT max(id) FROM cloud.snapshots) + 1, 1) WHERE name='snapshots_seq'