/*
 * AMW - Automated Middleware allows you to manage the configurations of
 * your Java EE applications on an unlimited number of different environments
 * with various versions, including the automated deployment of those apps.
 * Copyright (C) 2013-2016 by Puzzle ITC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package ch.puzzle.itc.mobiliar.business.database.control;

import ch.puzzle.itc.mobiliar.business.database.entity.SequencesEntity;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import java.util.logging.Logger;

/**
 * Services for sequences
 * 
 * @author cweber
 */
@Stateless
public class SequencesService {
	@Inject
	EntityManager em;

	@Inject
	Logger log;

	/**
	 * Returns the nextValue for the sequence with the given name and increments the value on db.
	 * 
	 * @param sequenceName
	 * @return
	 */
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public Integer getNextValueAndUpdate(String sequenceName) {

	    SequencesEntity lockedSeq = em.find(SequencesEntity.class, sequenceName, LockModeType.PESSIMISTIC_WRITE);
	    Integer nextVal = lockedSeq.getNextValue();
	    lockedSeq.setNextValue(nextVal + 1);
         return nextVal;
	}

}

@@ -1559,16 +1559,20 @@
 ents the
+%0A%09 *
  value o
@@ -1577,16 +1577,16 @@
  on db.%0A
-
 %09 * %0A%09 *
@@ -1748,20 +1748,17 @@
 me) %7B%0A%0A%09
-    
+%09
 Sequence
@@ -1856,20 +1856,17 @@
 RITE);%0A%09
-    
+%09
 Integer 
@@ -1902,20 +1902,17 @@
 lue();%0A%09
-    
+%09
 lockedSe
@@ -1944,17 +1944,10 @@
 1);%0A
-         
+%09%09
 retu

format SequencesService.java