Created
June 1, 2010 15:42
-
-
Save dbonillaf/421067 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.six.test; | |
import javax.persistence.EntityManager; | |
import javax.persistence.Query; | |
import com.six.model.entity.*; | |
import javax.ejb.EJB; | |
import javax.ejb.Stateless; | |
import javax.ejb.TransactionAttribute; | |
import javax.ejb.TransactionAttributeType; | |
import javax.persistence.NoResultException; | |
import javax.persistence.PersistenceContext; | |
import org.eclipse.persistence.config.PessimisticLock; | |
import org.eclipse.persistence.config.QueryHints; | |
/** | |
* @author Spiderman | |
*/ | |
@Stateless | |
@TransactionAttribute(TransactionAttributeType.REQUIRED) | |
public class ActualizaCliente { | |
private static final int CUSTOMER_ID = 1; | |
private static final int SECONDS_WAIT = 2; | |
@PersistenceContext | |
private EntityManager em; | |
@EJB | |
private NOActualizaCliente sc2; | |
public ActualizaCliente() { | |
} | |
public void renombra(String nombre, int tipoBloqueo) throws Throwable { | |
try { | |
Query q = getQuery(tipoBloqueo); | |
Customer c = (Customer) q.getSingleResult(); | |
System.out.println("El nombre del Cliente es: " + c.getName()); | |
if (c == null) { | |
// Crea un nuevo Cliente | |
Customer customer0 = new Customer(); | |
customer0.setId(CUSTOMER_ID); | |
customer0.setName("Jose María Arranz"); | |
// Persiste el cliente | |
em.persist(customer0); | |
// Crea 2 nuevas ordenes | |
Order order1 = new Order(); | |
order1.setId(100); | |
order1.setAddress("C/ACDC 14. Leganes, España"); | |
Order order2 = new Order(); | |
order2.setId(200); | |
order2.setAddress("129 James Gosling Ave. San Francisco, USA"); | |
/** | |
* Asociación de las ordenes con el cliente. La asociación | |
* debe estar configurada en ambos lados de la relación: en | |
* la parte del Cliente para que las ordenes se persistan | |
* cuando la transaccion se comitee, y en el lado de las | |
* ordenes para configurar la relación. | |
*/ | |
customer0.getOrders().add(order1); | |
order1.setCustomer(customer0); | |
customer0.getOrders().add(order2); | |
order2.setCustomer(customer0); | |
} else { | |
sc2.renombra(c); | |
c.setName(nombre); | |
em.flush(); | |
System.out.println("El nombre del Cliente es: " + c.getName()); | |
} | |
} catch (Throwable e) { | |
System.out.println("ERROR: " + e.getMessage()); | |
throw e; | |
} | |
} | |
private Query getQuery(int tipoBloqueo) { | |
// Por defecto, se utiliza el bloqueo optimista. | |
// Si se actualiza despues de otra actualización OptimisticLock Exception | |
Query q = em.createNamedQuery("Customer.findById"); | |
q.setParameter("id", CUSTOMER_ID); | |
switch(tipoBloqueo) { | |
case 1: | |
// Bloqueo pesimista con no wait | |
q.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.LockNoWait); | |
System.out.println("Bloqueos Pesimistas con NOWAIT !!!"); | |
break; | |
case 2: | |
//Bloqueo pesimista con wait de 2 segundos | |
q.setHint(QueryHints.PESSIMISTIC_LOCK, PessimisticLock.Lock); | |
q.setHint(QueryHints.JDBC_TIMEOUT, SECONDS_WAIT); | |
System.out.println("Bloqueos Pesimistas con WAIT de " + SECONDS_WAIT + " segundos !!!"); | |
break; | |
default: | |
System.out.println("Bloqueos Optimistas !!!"); | |
break; | |
} | |
return q; | |
} | |
private Customer findCustomer(Integer id) { | |
Customer customer = null; | |
Query q = em.createQuery("select c from Customer c where c.id = :id"); | |
q.setParameter("id", id); | |
try { | |
customer = (Customer) q.getSingleResult(); | |
} catch (NoResultException e) { | |
} | |
System.out.println(customer == null ? "Customer nulo " : "Customer " + customer.getName()); | |
return customer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment