Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Problems with UnitOfWork
Problems with UnitOfWork [message #383812] Thu, 20 November 2008 14:01 Go to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
Hi, I'm trying to figure out how EclipseLink's UnitOfWork works, but I'm
having problems with the most basic tasks. The manual explains how UOWs
should be used, but I'm unable to get the examples working. What I'm
trying to do, is to simply insert a new object to the database. The
following code snippet works just fine.

-------
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("default", properties);
EntityManager em = emf.createEntityManager();

Greeting g = new Greeting();
g.setMessage("hello world");
em.getTransaction().begin();
em.persist(g);
em.getTransaction().commit();
-------

But once I try to do the same with a UOW, then it doesn't work, doesn't
give any errors - nothing - simply doesn't work.

-------
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("default", properties);
EntityManager em = emf.createEntityManager();

Session session = ((JpaEntityManager) em.getDelegate()).getSession();

UnitOfWork uow = session.acquireUnitOfWork();
uow.beginEarlyTransaction();
Greeting greeting = new Greeting();
Greeting greetingClone = (Greeting) uow.registerObject(greeting);
greetingClone.setMessage("hello world");
uow.commit();
-------

So, what am I doing wrong?
Re: Problems with UnitOfWork [message #383813 is a reply to message #383812] Thu, 20 November 2008 14:14 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

In general you should not need to use the UnitOfWork, just the
EntityManager.

However, nothing looks incorrect with your code, perhaps turn logging to
finest and see what is occurring.

Some possibilities could be:
- Are you using JTA, if so then the uow.commit() will do nothing, it will
commit when the JTA transaction commits.
- registerObject can be used for new or existing objects, if the uow
thinks your object is existing (already an object with the same primary
key in the cache) then it will register the existing object, and detect no
changes, so do nothing. Also note that registerObject returns a clone
that must be used for any further changes. You could try
registerNewObject() which does not clone, and is for new objects only.
- The beginEarlyTransaction() should not be required, but should not be
causing any issue.

---
James
http://www.nabble.com/EclipseLink---Users-f26658.html


James : Wiki : Book : Blog : Twitter
Re: Problems with UnitOfWork [message #383814 is a reply to message #383813] Fri, 21 November 2008 06:59 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
Hi

I have a need for parallel transactions and that's why I'm trying to use a
UnitOfWorks.

Enabling the logging will give the following result
[EL Finest]: 2008.11.21
08:55:55.731--UnitOfWork(28218271)--Thread(Thread[main,5,mai n])--Register
the object playground.data.Greeting@9722c9
[EL Finer]: 2008.11.21
08:55:55.743--UnitOfWork(28218271)--Thread(Thread[main,5,mai n])--begin
unit of work commit
[EL Finer]: 2008.11.21
08:55:55.763--UnitOfWork(28218271)--Thread(Thread[main,5,mai n])--end unit
of work commit
[EL Finer]: 2008.11.21
08:55:55.768--UnitOfWork(28218271)--Thread(Thread[main,5,mai n])--release
unit of work


My application is a EE and it is deployed on tomcat 6.0. Transaction-type
is defined as "RESOURCE_LOCAL" in the persistence.xml

The properties given to the createEntityManagerFactory-method are as
follows

properties.put("eclipselink.jdbc.user", "xxxxxxx");
properties.put("eclipselink.jdbc.password", "xxxxxxxx");
properties.put("eclipselink.jdbc.url","jbdc:postgresql://localhost/playground");
properties.put("eclipselink.jdbc.driver", "org.postgresql.Driver");
properties.put("eclipselink.ddl-generation", "drop-and-create-tables");
properties.put("eclipselink.ddl-generation.output-mode", "database");
Re: Problems with UnitOfWork [message #383819 is a reply to message #383814] Mon, 24 November 2008 14:22 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

From the log it looks like the object is detected to be an existing object
(has the same primary key as another registered or cached object). Check
its primary key, also try registerNewObject() to force it as new.


James : Wiki : Book : Blog : Twitter
Re: Problems with UnitOfWork [message #383822 is a reply to message #383819] Tue, 25 November 2008 06:35 Go to previous messageGo to next message
No real name is currently offline No real nameFriend
Messages: 6
Registered: July 2009
Junior Member
I tried what you suggested. I debugged it and both the original and the
clone object were empty (no primary key set). The database table was empty
as well. I run my example code right after startup, with a clean database,
so there shouldn't be any PK conflicts or cached objects. Changing the
registration of the object to use registerNewObject() didn't help.

Any other ideas? Below is the logs after the suggested changes.

[EL Finest]: 2008.11.25
08:32:27.345--UnitOfWork(13482579)--Thread(Thread[main,5,mai n])--Register
the new object playground.data.Greeting@5539d8
[EL Finer]: 2008.11.25
08:32:27.359--UnitOfWork(13482579)--Thread(Thread[main,5,mai n])--begin
unit of work commit
[EL Finer]: 2008.11.25
08:32:27.392--UnitOfWork(13482579)--Thread(Thread[main,5,mai n])--end unit
of work commit
[EL Finer]: 2008.11.25
08:32:27.393--UnitOfWork(13482579)--Thread(Thread[main,5,mai n])--release
unit of work
Re: Problems with UnitOfWork [message #383824 is a reply to message #383822] Tue, 25 November 2008 14:38 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Doesn't make much sense, I have never seen this before, you will probably
have to debug it. Put a break point in commit, and see what is going on.

The only other possibility I can think of, is you have a nested UnitOfWork
(did you acquire the UnitOfWork from another UnitOfWork?). A nested
UnitOfWork will not commit to the database, only to its parent.


James : Wiki : Book : Blog : Twitter
Previous Topic:JPA/EclipseLink "abuse": table rows -> hash map?
Next Topic:Ñ character mapping problem
Goto Forum:
  


Current Time: Thu Apr 25 23:37:15 GMT 2024

Powered by FUDForum. Page generated in 0.03120 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top