/**
* This file is part of jpa-cert application.
*
* Jpa-cert is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Jpa-cert 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with jpa-cert; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.kaczmarzyk.jpacert.service.locking;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import net.kaczmarzyk.jpacert.domain.locking.Document;
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class DocumentService {
@PersistenceContext
private EntityManager em;
public Document newDoc(String content) {
Document doc = new Document(content);
em.persist(doc);
return doc;
}
public void append(Long id, String text) {
Document doc = em.find(Document.class, id);
doc.append(text);
}
public void deleteAll() {
em.createQuery("delete from Document").executeUpdate();
}
public Document readWithForceIncrement(Long id) {
return em.find(Document.class, id, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
}
}