-->
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.  [ 6 posts ] 
Author Message
 Post subject: Id is null even after saving an object
PostPosted: Wed Aug 06, 2008 9:01 am 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi all,

When I persist an item in the database, I have a log indicating my save/create methods are executing asynchronously and they are also executed after the entire enclosing method has been called.

For example:
Code:
public void registerNewUser(User user)
{
  userDao.create(user);
 
  sendNewUserRegistrationEmail(user.getId());
}



When I attempt to send a new user registration email, it fails because the newly created user has not been created just yet. And, it's userId is still null at that point in time.

Is this a problem with my implementation? How can I get the create method to return after the user has been created and the transaction committed? I thought it was supposed to work that way.


Thanks,
Walter


Top
 Profile  
 
 Post subject: Details
PostPosted: Wed Aug 06, 2008 10:46 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
What is your UserDao#create() doing? Does it calls commit() when it finishes?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:12 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi,

Thanks for your reply.

You helped me realize that I have auto commit turned off. I am doing that for one main reason, if something fails inside the manager that is calling this method, then I want to rollback the transaction.

I thought that was the problem before, so what I did was separate functionality out into several methods.

If I want to keep the rollback functionality, how can I ensure I commit the transaction before another method is executed?

I guess what I did is essentially allow the commit part to run in a separate thread and since it's in a separate thread, I cannot control the exact timing of that.


Walter


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 06, 2008 12:55 pm 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
Are you using any JTA provider, to control the transactions in the container? Also, what is the type of the ID you configured for your persistent object?

I'd recommend that you add transaction demarcations on your business method, so you can call the rollback if anything goes wrong. By the way: it is recommended to always add the transactions demarcations, even for read only operations :-)

But I didn't understand why you want to keep the commit part to be in another thread. As far as I know, the transactions are attached to a single thread, and is needed to pass the transaction itself to another thread if you want to use it there. As far as I understand, this is only needed in some specific cases.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 09, 2008 11:34 am 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi,

Thanks for your reply. I am using JpaTransactionManager:
Code:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
   </bean>
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <tx:attributes>
         <tx:method name="find*, read*, get*" read-only="true" />
         <tx:method name="save, create, delete, remove, update" rollback-for="Throwable" />
         <tx:method name="createImmediately"/>
      </tx:attributes>
   </tx:advice>
   <aop:config>
      <aop:pointcut id="coreDefaultOperation" expression="execution(* com.walterjwhite.manager.*.*(..))" />
      <aop:advisor pointcut-ref="coreDefaultOperation" advice-ref="txAdvice" />
   </aop:config>
   <!--
      Instruct Spring to perform declarative transaction management
      automatically on annotated classes.
      transaction-manager="transactionManager"
   -->
   <tx:annotation-driven transaction-manager="transactionManager" />



I configured that in my applicationContext.xml so that certain methods in my manager will have read only transactions and others that perform saves, updates, or deletes will be read/write. I'm not sure entirely how it works, but since the timing was delayed, that is why I made the observation that it must be running in a separate thread which I don't want. I want the methods to be executed in the order I have them written. First save the object, 2 get the id of the object and schedule a job in quartz with that.

Should I use JtaTransactionManager? Eventually, we would like this system to span multiple machines so we'll probably need to switch to that at some point.


Thanks,
Walter


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 12, 2008 1:47 pm 
Beginner
Beginner

Joined: Thu Feb 21, 2008 3:31 pm
Posts: 34
Hi all,

I was doing some more reading that perhaps I need to use spring tomcat weaver? I am using a JPA implementation, but I'm using aspectj to compile all the classes so shouldn't that do it?

Any thoughts if that will work? This is the URL:

http://forum.springframework.org/archiv ... 28028.html

I am using hibernate for the persistence layer, but am using the JPA implementation. Is this correct to assume that I need to use Spring Tomcat Weaver in order for this to be properly configured and behave as expected?


Walter


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.