What is persistence contexts collision?

I'm trying to learn more on persistence in EJB, specially about persistence context. I know than a persistence context is created when a method than requires a transaction have been called and no persistence context existed before. I also know than a persistence context is destroyed when there is no method in action than requires a transaction, except if the EntityManager scope is extended. In this case, the persistence context will be destroyed when the session bean than contains the EntityManager is going to be destroyed. I read "Pro EJB 3 Java Persistence API" by Mike Keith and Merrick Schincariol. In this e-book, it mention than two persistence contexts can collide. Collisions between persistence contexts happen when we use different kinds of EntityManager. This very special part of the book brought to me a lot of questions. I want to know more about persistence context, so these are my questions :
- First of all, how many persistence contexts from the same persistence unit (EntityManagerFactory) can exist at the same time? If more than one is possible, in which context it is possible and how the propagation works when it happen?
- Second, when I'm using a new EntityManager while a persistence context created by the same persistence unit exist, is this new EntityManager will be related to the persistence context than already exists?
- Finally, what is a collision of persistence contexts? When two persistence contexts collide? What are the consequences?
- For example, if I use a stateful session bean with an extended scope EntityManager, then I call a method of this stateful session bean than call a method of a stateless session bean, and the last method use a transaction scope EntityManager, can I get a collision of two persistence contexts? I mean, will the persistence context in the stateful session bean collide with the persistence context in the stateless session bean?
Thank you in advance from your answer.
Dominic
P.S. You can answer to me in french if it's easier for you.

morphian wrote:
Hi,
I'm a total noob. I've been reading and following examples from books but I haven't read anything about how persistence really works? How does it help my session beans to access or make transactions to the database?
How does it actually work? What if I just make direct queries from my session beans without the help of the EntityManager or the Peristence API? What's the difference?
You have to code the stuff that EntityManager and Persistence are doing for you.
Like this for example:
Customer requestCustomer = em.find(Customer.class.customerId);Does it mean that the Persistence API keeps the object alive in the Customer Entity? For how long? Until the application is closed? Complicated. Not enough info.
If so, what happens if the user opens the application again and looks for that object?
Open the app and the object will be refreshed from that data store. Hence the word "persistent".
Why can't I just make a direct query to see if that customerId exists in the database?
You can.
I hope you guys don't mind my questions... I just really want to learn this stuff and I gotta let things out of my head. Hope you can help. Thanks!This isn't the way to do it, but it's a start.
%

Similar Messages

  • Is Persistence context thread safe?  nop

    In my code, i have a servlet , 2 stateless session beans:
    in the servlet, i use jndi to find a stateless bean (A) and invoke a method methodA in A:
    (Stateless bean A):
    @EJB
    StatelessBeanB beanB;
    methodA(obj) {
    beanB.save(obj);
    (Stateless bean B, where container inject a persistence context):
    @PersistenceContext private EntityManager em;
    save(obj) {
    em.persist(obj);
    it is said entity manager is not thread safe, so it should not be an instance variable. But in the code above, the variable "em" is an instance variable of stateless bean B, Does it make sense to put it there using pc annotation? is it then thread safe when it is injected (manager) by container?
    since i think B is a stateless session bean, so each request will share the instance variable of B class which is not a thread safe way.
    So , could you please give me any guide on this problem? make me clear.
    any is appreciated. thank you
    and what if i change stateless bean B to
    (Stateless bean B, where container inject a persistence context):
    @PersistenceUnit private EntityManagerFactory emf;
    save(obj) {
    em = emf.creatEntityManager()
    //begin tran
    em.persist(obj);
    // commit tran
    //close em
    the problem is i have several stateless beans like B, if each one has a emf injected, is there any problem ?

    Hi Jacky,
    An EntityManager object is not thread-safe. However, that's not a problem when accessing an EntityManager from EJB bean instance state. The EJB container guarantees that no more than one thread has access to a particular bean instance at any given time. In the case of stateless session beans, the container uses as many distinct bean instances as there are concurrent requests.
    Storing an EntityManager object in servlet instance state would be a problem, since in the servlet programming model any number of concurrent threads share the same servlet instance.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Extended Persistence Context And user transaction

    Hello
    I use Extended persistence context with stateful session bean in my app, but when i persist object it does not store in database,i dont know whats wrong:
    @Stateful
    @TransactionManagement(TransactionManagementType.BEAN)
    public class ProfessionAction implements ProfessionLocal {
         @PersistenceContext(type = PersistenceContextType.EXTENDED)
         private EntityManager em;
         @Transient
         private Logger log;
         @Resource
         private UserTransaction ut;
         public Profession add( Profession p) {
              try {
                   log.info("Adding Profession...");
                   ut.begin();
                   em.persist(p);
                   ut.commit();
                   log.info("Profession Added...");
              } catch (Exception ex) {
                   log.error(ex.getMessage(), ex);
              return p;
         }

    What if you change the code to this:
    @Stateful
    @TransactionManagement(TransactionManagementType.BEAN)
    public class ProfessionAction implements ProfessionLocal {
    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;
    @Transient
    private Logger log;
    public Profession add( Profession p) {
    try {
    log.info("Adding Profession...");
    em.getTransaction().begin();
    em.persist(p);
    em.getTransaction().commit();
    log.info("Profession Added...");
    } catch (Exception ex) {
    log.error(ex.getMessage(), ex);
    return p;
    }?

  • EJB 3.0 Locking (entity not in the persistence context)

    hi all,
    i have some problems about locking,
    i have 1 remote and one local interface
    1. Remote Interface
    public @Stateful
    @Remote(RemoteInterface.class)
    class RemoteInterfaceBean implements RemoteInterface
           @PersistenceContext(unitName = "CustomerCareOracle")
         private EntityManager oracleManager;
            public MySomeEntityObject object;
           @EJB
            MyLocalInterface inter;   
        @TransactionAttribute(TransactionAttributeType.REQUIRED)
        public void fillMyObject(someparameters .... )
          // filling MySomeEntityObject object
        @TransactionAttribute(TransactionAttributeType.REQUIRED)
        public void commitMyObject()
          inter.AnotherOperatOnObject(object);
          oracleManager.persist(object);
    2. My Local Interface
    public @Stateless
    @Local(MyLocalInterface.class)
    class MyLocalInterfaceBean implements MyLocalInterface
           @PersistenceContext(unitName = "CustomerCareOracle")
         private EntityManager oracleManager;
            @TransactionAttribute(TransactionAttributeType.REQUIRED)
             public void AnotherOperatOnObject(MySomeEntityObject object)
                   oracleManager.lock(object,LockModeType.READ);
    // <= Here I Got An Error
                   // do some operation and is succsess, It's very good ;)
    }     when i tryed to lock object i got an error, i can't resolve this problem :(
    can anybody help me ?
    error trace
    17:43:21,343 ERROR [errorCat] java.lang.IllegalArgumentException: entity not in the persistence cont
    ext
            at org.hibernate.ejb.AbstractEntityManagerImpl.lock(AbstractEntityManagerImpl.java:336)
            at org.jboss.ejb3.entity.TransactionScopedEntityManager.lock(TransactionScopedEntityManager.
    java:101)
            at com.magti.businesslayer.ejb3Fasade.oracle.ccare.TransactionFasadeBean.lockAccounts(Transa
    ctionFasadeBean.java:203)
            at com.magti.businesslayer.ejb3Fasade.oracle.ccare.TransactionFasadeBean.doAfterCheck(Transa
    ctionFasadeBean.java:76)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
            at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
            at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor
    .java:63)
            at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
            at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPer
    sistenceContextPropagationInterceptor.java:57)     Regards,
    Paata.

    It looks like you're trying to call lock before the entity has been created within the persistence
    context. Persist the entity within the persistence context before calling lock.

  • Transactions, Persistence Contexts and Entity Managers... Oh My!

    Hi
    I am just getting into J2EE and EJB3. I have no prior experience with EJB2, we avoided it with our own POJO data access framework. With the advent of EJB3 we would like to use it in a new application.
    I am reading the book "Pro EJB 3" by Ketih and Schincariol and am having trouble wrapping my head around then Entity Manager chapter. I don't think it's any fault of the authors, but somehow it's just not coming together in my head. Perhaps it's because I have no real prior EJB experience.
    Can anyone point me to a resource that gives an overview of the relationships between Transactions, Persistence Contexts and Entity Managers? Or perhaps I just have to keep mulling it over until it all comes together...?
    Thanks in advance!

    Hi Shelli,
    I haven't read that book but I don't know of a good writeup of the tx topic specifically outside
    of the spec itself. There are certainly a lot of combinations, between container-managed
    vs. bean-managed EntityManagers, and JTA vs. resource-local. However, the
    common usage case within Java EE -- container-managed and JTA -- is very simple.
    In this case, the container automatically associates the persistence context with the
    active global transaction. If you make a call to another Java EE component within the
    same transaction and that component accesses a container-managed EntityManager
    for the same persistence unit, the persistence context is automatically propagated so
    that both components "see" the same persistence context.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • What is External Context Mapping

    Hai Experts,
    I have a general question. What is " External Context Mapping " & I want some Tutorials on it to know how it is Useful in Application Development.
    Thanks and Regards,
    Roop Kumar.

    Hi,
    Go through this article Componentization
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/f727d100-0801-0010-4cbd-b0ad5c161945
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/e1f4df00-0801-0010-6298-8fdf667bbd0f
    New Web Dynpro Java Tutorial - Component Interface Definitions in Practice
    Regards
    Ayyapparaj

  • Why not use EXTENDED persistence context?

    When would you not ever use EXTENDED persistence context in a stateful session bean (which is the only EJB type it can be used in)?
    To me it seems that the code is cleaner (no unnecessary merge() or whatever) and it only makes sense as a stateful session bean is used to represent conversational state over multiple client calls...by setting an EXTENDED persistence context you recognize that there is a conversation happening.
    I'm not seeing when you'd not want to use EXTENDED persistence context (I understand it's not the default persistence context).
    Your thoughts please...
    Thanks.

    For the use-case you mentioned an extended persistence context is a natural fit.
    It really depends on whether the semantics of your SFSB operations call for
    the references to the same entity objects to be maintained across transactions.
    E.g., you might have an SFSB whose state is not derived from persistence state
    but which just occasionally makes a database query as part of some of its
    business operations. In that case there wouldn't be any reason to use an
    extended persistence context.
    --ken                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • What is a context builder SE33

    Hello SDNers,
    What is a context builder SE33 and why is it used and where.
    If I would get some sample examples it would help me.
    Thanks and Regards,
    Ranjith N

    Hi,
    Check
    http://help.sap.com/saphelp_nw70/helpdata/en/9f/db961235c111d1829f0000e829fbfe/content.htm
    Edited by: Neenu Jose on Nov 18, 2008 10:18 AM

  • What is the context

    Hi,
    What is the context in Webdypro ABAP, can you brief me ?
    Regards,
    Gopal

    Hi Kishore,
    The hierarchical storage for the global variables of controllers is called the context.
    Context in Webdynpro which hold the Nodes and attrubutes which can be used to bing to the UI element.
    -> Link a data container that holds the data
    -> The data transport between the controller can be established with mapping definition.
    The variables defined in a Web Dynpro Controller context can be referenced from
    other Web Dynpro controllers. This is called context mapping. This allows to
    share common attributes between different controllers, so copying these attributes
    between the controller contexts is not necessary.
    The value of UI elements, that allow a user input, have to be connected to context
    attributes of the corresponding controller. This is called data binding. Through
    data binding, an automatic data transport between the UI elements and the context
    attributes is established.
    Combining these two concepts, the data transport between UI elements located in
    different views can be defined in a purely declarative way.
    Warm Regards,
    Vijay

  • What is Cross Context session.

    HI,
    What is Cross Context session?
    Iam using Tomcat .
    pls help
    Thanks in advance
    arvin

    Where have u heard about that concept? I don't know it.
    I know about "crossContext" feature of Tomcat which means other applications can gain access to ServletContext of your application (by using the appropiate API), but did know nothing about "cross context sessions"...
    Sorry.

  • What Is AutoConfig,Context File,ATG?

    Hi...
    What Is AutoConfig ,Context File, ATG ?
    With Brief Definition..
    Regards
    **SBJ**

    Hi,
    Please refer to:
    Note: 218089.1 - Autoconfig FAQ
    ATG
    Re: ATG
    Please search the forum before posting similar questions.
    Thanks,
    Hussein

  • Persistence Context In EJB 3.0

    I created an EJB 3.0 project in JDeveloper 10.1.3.3.0 and everything works fine in the embedded OC4J server. I am deploying the application to an OC4J instance consisting of 3 Java Virtual Machines in Oracle Application server 10.1.3 and seem to be getting a different persistence context for each virtual machine. i.e. I'll have 3 browsers that can view the same data even after updating, another 2 browsers that can't see the first 3's changes but can see changes by any browser in this group, etc.
    I tried adding the below to the persistence-unit tag in the persistence.xml but it didn't seem to help.
    <properties>
    <property name="toplink.cache.shared.default" value="true"/>
    </properties>
    How do you get all connections to use the same cache?

    Hi,
    I am afraid, but this seems to be less a question for the JDeveloper forum but OC4J
    OC4J
    Frank

  • What is persistence?  How does it work?

    Hi,
    I'm a total noob. I've been reading and following examples from books but I haven't read anything about how persistence really works? How does it help my session beans to access or make transactions to the database? How does it actually work? What if I just make direct queries from my session beans without the help of the EntityManager or the Peristence API? What's the difference?
    Like this for example:
    Customer requestCustomer = em.find(Customer.class.customerId);Does it mean that the Persistence API keeps the object alive in the Customer Entity? For how long? Until the application is closed? If so, what happens if the user opens the application again and looks for that object?
    Why can't I just make a direct query to see if that customerId exists in the database?
    I hope you guys don't mind my questions... I just really want to learn this stuff and I gotta let things out of my head. Hope you can help. Thanks!

    morphian wrote:
    Hi,
    I'm a total noob. I've been reading and following examples from books but I haven't read anything about how persistence really works? How does it help my session beans to access or make transactions to the database?
    How does it actually work? What if I just make direct queries from my session beans without the help of the EntityManager or the Peristence API? What's the difference?
    You have to code the stuff that EntityManager and Persistence are doing for you.
    Like this for example:
    Customer requestCustomer = em.find(Customer.class.customerId);Does it mean that the Persistence API keeps the object alive in the Customer Entity? For how long? Until the application is closed? Complicated. Not enough info.
    If so, what happens if the user opens the application again and looks for that object?
    Open the app and the object will be refreshed from that data store. Hence the word "persistent".
    Why can't I just make a direct query to see if that customerId exists in the database?
    You can.
    I hope you guys don't mind my questions... I just really want to learn this stuff and I gotta let things out of my head. Hope you can help. Thanks!This isn't the way to do it, but it's a start.
    %

  • What is servelt context

    Hi All,
    I know servlet context is used to interact with its environment.What does it mean.
    Please can any one help me.

    Hi,
    Context means your web application.
    For example, on 1server Tomcat or any Other,
    we can have many web applications.
    --Paul.                                                                                                                                                                                                                                                                   

  • What is web context in java?

    I am now programming a set of servlets sharing some objects
    through ServletContext. And I am curious about what is ServletContext?
    It seems that the ServletContext is unique to a web application.
    But, then, what is web application? Is it a set of servlets?
    Then, who and how define the set?
    Or... is the ServletContext something accessable from 'all' servlets
    running in a web container (whoever made and initiated them)?
    Thanks in advance.

    A web application is, roughly spoken, the collection of all html, jsp, and image files and java classes that make up one independent application or service available through www. This concept is used in order to insulate different web applications and make it possible to deploy several of them on the same servlet engine, in the same VM and even under the same (virtual) http server. Each web application has a path of its own. For example you could have a shop application starting with the URL path "/shop/" and a newsticker application starting with the URL path "/news/". Although both applications are deployed to the same server, they're insulated and may have individual settins (through a web.xml file for example).
    Web applications are typically deployed by either packing them to a war file (just a jar file with suffix ".war") and placing this to some webapps directory, or by defining web applications in the servlet engine's settings and putting all the files into a corresponding document root. Read the docs of your servlet engine (e.g. Tomcat) for details. By the way, your servlet engine is the "web container".
    Regards

Maybe you are looking for

  • How to remove duplicates in iphoto 7.1.5 and aperture 2.1.4 on same hard drive

    How to remove duplicates from iPhoto 7.1.5 and Aperture 2.1.4 on same hard drive?

  • ORA-00020 error, max connections reached

    I tried logging into TOAD from back-end but it started giving error like 'ORA-00020, maximum no of connections exceeded...'. What, are the steps we can take to counter this. I hope, my question is clear. Please, help in solving the doubt. regards

  • Is there a way to transfer my bookmarks into footers?

    I have a 171 page .pdf document that started out as a combination of Word documents and jpegs.  Almost every page is bookmarked.  I need to print the .pdf and want the bookmarks printed as a footer to each corresponding page.  Can that be done?

  • Registers in SAP

    Hi Friends,                   can anybody tell us that what are the statutory requirements will be fulfilled by sap ecc 6.0 like vat service tax register, rg23a,23c,rg1,cenvat register, 57ae...............

  • Walkman widget doesn't appear in lockscreen

    Hello, Today I've updated the phone to lollipop and Walkman widget doesn't appear in the lockscreen. The lockscreen has the album art applied correctly but I don't have the widget to pause or to choose the next music. I have Xperia z1 compact