-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Throw an exception dosnt cause to rollbace when it is not
PostPosted: Wed Jun 13, 2007 1:09 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
at the same EJB

I have a facade (Stateless) and a DAO (stateless)

Code:
@Stateless
@Local(HotelService.class)
@Remote(HotelService.class)
public class HotelServiceBean implements HotelService {


    @EJB
    private HotelDao hotelDao;

    public List<Hotel> findAllHotels() {
        return hotelDao.findAllHotels();
    }

    public Hotel saveHotel(Hotel newHotel) {
       Hotel hotel =  hotelDao.saveHotel(newHotel);
       String test = null;
       if(test == null){
          throw new RuntimeException();
       }
        return hotel;
    }

}


As you can see after the dao save the record i throw an exception , but the record was save to the DB and roll back was not executed.

DS:

Code:
<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: mysql-ds.xml 41016 2006-02-07 14:23:00Z acoliver $ -->
<!--  Datasource config for MySQL using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->

<datasources>
<local-tx-datasource>   
    <jndi-name>TestDS</jndi-name>
    <connection-url>jdbc:mysql://localhost:3306/test</connection-url>
    <driver-class>com.mysql.jdbc.Driver</driver-class>
    <user-name>test</user-name>
    <password>1234</password>
    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name>
    -->
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
       <type-mapping>mySQL</type-mapping>
    </metadata>
</local-tx-datasource>   
</datasources>


Perssistence.xml

Code:
<persistence>
  <persistence-unit name="Demo">
    <jta-data-source>java:/TestDS</jta-data-source>
    <properties>
      <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
  </persistence-unit>
</persistence>



Thank you very much!!!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 3:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
My guess: Hoteldao should be a local EJB not a remote

_________________
Emmanuel


Top
 Profile  
 
 Post subject: I tried the same scenario in DAO that it is a pojo class
PostPosted: Wed Jun 13, 2007 4:58 pm 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
I create the entity manager factory one time by using in the Persistence class.

Than in the DAO create the entity manager and still after call to the persist the object is persist and commit to the DB.
If i throw an exception after the persist it dosnt roll back.

Thank you!!!


Top
 Profile  
 
 Post subject: The dao is local in my test
PostPosted: Thu Jun 14, 2007 1:06 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 6:54 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
Does it mean, that the hotelDao.saveHotel(newHotel); call works in it own persistence and transaction context, while HotelServiceBean.saveHotel(Hotel newHotel) has another persistence context and transaction ?


Top
 Profile  
 
 Post subject: Here is the DAO
PostPosted: Thu Jun 14, 2007 7:45 am 
Regular
Regular

Joined: Wed May 02, 2007 2:42 pm
Posts: 101
@Stateless
@Local(HotelDao.class)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class HotelDaoImpl implements HotelDao{
@PersistenceContext(unitName="Demo")
private EntityManager em;

public Hotel saveHotel(Hotel newHotel) {
em.persist(newHotel);
return newHotel;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 14, 2007 8:08 am 
Beginner
Beginner

Joined: Mon Sep 12, 2005 3:27 am
Posts: 48
This doesn't answer my question. But i believe i have the answer now:

if you use a remote interface on server-side, then this interface will return a deteached entity, so you have to merge it first, to attach it to your persistence context and transaction.

Only the local interface will return a attached entity, which can be rollbacked by transaction-rollback.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.