This discussion is archived
1 Reply Latest reply: Apr 10, 2012 8:05 PM by ArunBodap RSS

Entity bean creates a transaction when reading from the database?

929017 Newbie
Currently Being Moderated
Hello,

I have a stateless session bean which uses a BMP entity bean to retrieve data from the database.

My stateless session bean has a method load() which calls getValues() on the entity bean.

Analyzing logs and stepping thru the code, I found out that each and every getValues() call is demarcated by a transaction. I also know that the calling method load() is not in a transaction (used the TransactionHelper to figure that out). Basically a new transaction is created when getValues() is called, and committed when getValues() call is done.
I was under the impression that transactions are required only when data is written to a database (eg setValues()) but not when you read it. Even more, I have set the transaction attribute to "Never", and I am not getting any exceptions as I would have expected.

Am I doing something wrong in my setup or this is the expected behavior of the entity beans?


Here is the ejb-jar.xml config related to my question:

          <session>
               <display-name>MyEJB</display-name>
               <ejb-name>ejb/MyEJB</ejb-name>
               <home>com.example.MyEjbHome</home>
               <remote>com.example.MyEjb</remote>
               <ejb-class>com.example.MyEjbBean</ejb-class>
               <session-type>Stateless</session-type>
               <transaction-type>Container</transaction-type>
               <ejb-ref>
                    <ejb-ref-name>ejb/MyEntityBean</ejb-ref-name>
                    <ejb-ref-type>Entity</ejb-ref-type>
                    <home>com.example.MyEntityHome</home>
                    <remote>com.example.MyEntity</remote>
                    <ejb-link>MyEJB.jar#ejb/MyEntityBean</ejb-link>
               </ejb-ref>
               <resource-ref>
                    <res-ref-name>jdbc/MyDB</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <res-auth>Container</res-auth>
                    <res-sharing-scope>Shareable</res-sharing-scope>
               </resource-ref>
          </session>


          <entity>
               <display-name>MyEntityEJB</display-name>
               <ejb-name>ejb/MyEntityBean</ejb-name>
               <home>com.example.MyEntityHome</home>
               <remote>com.example.MyEntity</remote>
               <ejb-class>com.example.MyEntityBean</ejb-class>
               <persistence-type>Bean</persistence-type>
               <prim-key-class>com.example.MyEntityPK</prim-key-class>
               <reentrant>False</reentrant>
               <resource-ref>
                    <res-ref-name>jdbc/MyDB</res-ref-name>
                    <res-type>javax.sql.DataSource</res-type>
                    <res-auth>Container</res-auth>
               </resource-ref>
          </entity>

          <container-transaction>
               <method>
                    <ejb-name>ejb/MyEntityBean</ejb-name>
                    <method-name>getValues</method-name>
               </method>
               <trans-attribute>Never</trans-attribute>
          </container-transaction>

Thank you for your help,
Seb

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points