users@glassfish.java.net

Re: how to create a MySql datasource using asadmin ?

From: Felipe Gaúcho <fgaucho_at_gmail.com>
Date: Sat, 15 Aug 2009 12:19:07 +0200

the persistenceunit is really necessary ? I really don't want to
handle transactions by my own.. I prefer the container to do
that....... (in general)..

On Fri, Aug 14, 2009 at 11:34 PM, Martin Gainty<mgainty_at_hotmail.com> wrote:
> you can't use @PersistenceContext to inject an EntityManager
>
> so you'll need to use @PersistenceUnit to inject an EntityManagerFactory
> and use lookup to PersistenceUnit to get hold of an EntityManager
>
> EntityManagerFactory example:
>
> public class RegistrationServlet extends HttpServlet {
> // inject EntityManagerfactory
> @javax.persistence.PersistenceUnit private EntityManagerFactory emf;
> @Resource private UserTransaction utx;
> public void service ( HttpServletRequest req , HttpServletResponse resp)
> throws ServletException, IOException {
>
> EntityManager em = emf.createEntityManager();
> try {
> ...
>
> utx.begin();
> em.persist(credential);
> utx.commit();
>
> ...
>
> } catch (Exception e)
> {
> try {
> utx.rollback();
> } catch (Exception e) {}
>
> }
> finally
> {
> em.close();
> }
> }
>
> or
> declare a dependency on an EntityManager from web.xml
>
> < persistence-context-ref>
> < persistence-context-ref-name>
> persistence/LogicalName
> </persistence-context-ref>
> < persistence-unit-name>
> ActualPUNameAsItAppearsInPersistence.xml
> </persistence-unit-name>
> </persistence-context-ref>
>
> then use JNDI to lookup
>
> public class RegistrationServlet extends HttpServlet {
>
> @Resource private UserTransaction utx;
>
> public void service ( HttpServletRequest req , HttpServletResponse resp)
>
> throws ServletException, IOException {
>
> Context envCtx = InitialContext().lookup("java:comp/env");
>
> EntityManager em = (EntityManager)
> envCtx.lookup("persistence/LogicalName");
> ...
> //start transaction
> utx.begin();
> //persist credential to DB
> em.persist(credential);
> //commit data
> utx.commit();
> ...
>
> }
> }
> http://weblogs.java.net/blog/ss141213/archive/2005/12/dont_use_persis_1.html
>
> Martin Gainty
> ______________________________________________
> Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
>
> Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene
> Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte
> Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht
> dient lediglich dem Austausch von Informationen und entfaltet keine
> rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von
> E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
>
> Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le
> destinataire prévu, nous te demandons avec bonté que pour satisfaire
> informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie
> de ceci est interdite. Ce message sert à l'information seulement et n'aura
> pas n'importe quel effet légalement obligatoire. Étant donné que les email
> peuvent facilement être sujets à la manipulation, nous ne pouvons accepter
> aucune responsabilité pour le contenu fourni.
>
>
>
>
>> Date: Fri, 14 Aug 2009 20:22:16 +0200
>> From: fgaucho_at_gmail.com
>> To: users_at_glassfish.dev.java.net
>> Subject: Re: how to create a MySql datasource using asadmin ?
>>
>> so, this code is thread safe:
>>
>> @PersistenceUnit(name = "footprint")
>> private EntityManagerFactory emf;
>>
>> public T read(final Serializable primaryKey) throws Exception {
>> EntityManager manager = emf.createEntityManager();
>> try {
>> return manager.find(entityClass, primaryKey);
>> } finally {
>> manager.close();
>> }
>> }
>>
>>
>> ??
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>
> ________________________________
> Get free photo software from Windows Live Click here.



-- 
Looking for a client application for this service:
http://fgaucho.dyndns.org:8080/footprint-service/wadl