Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Ignore Cache on Query

Hi Michael,

I feel a little bad for sort of hijacking this thread but I do want to finish this conversation.

When using JTA the transaction commits at the end of the method, but I don't want to wait that long and want to manually commit in the method.  For example, in the following example, I want the person to be written to database even though an exception is being thrown in the method.

public void createPerson(Person person) {
        em.persist(person);
        em.getTransaction().commit();
        throw new RuntimeException("I am intentionally throwing this exception");
}

The above code produces an exception: Cannot use an EntityTransaction while using JTA.

This sounds fair enough, but is there a workaround which will allow me to do this. I don't want to switch to using RESOURCE_LOCAL and have to manually commit everything all the time, I want to pick and choose as CMT suffices 95% of the time.

Thanks,
Zarar


On Tue, Jan 19, 2010 at 5:34 AM, Michael Simons <michael.simons@xxxxxxxxxxx> wrote:
Hello Zarar,

An em.flush() is less than an em.commit(), as it only runs the SQL against the database, in your
case the inserts, but does _not_ commit anything. That means nobody can see the new records
except the session, that created them (at least for isolation levels >= read committed, but
that's almost always the case). That's why you don't see the flushed records in the database.

If you set the logging level to Level.FINE or higher, the SQL statements are logged, so can
verify whether the em.flush() does what You want it to do.
(<property name="eclipselink.logging.level" value="FINE" />)

If you set em.flushMode to FlushModeType.COMMIT, nothing is written to the database until
em.commit() is called, so that's the opposite of what you want, isn't it?

I'm wondering what should be the difference between em.flush() (that _does_ a manual flush) and
the Hibernate thingy, because - as I mentioned above - whether the flushed records can be seen
outside the flushing transaction depends on the isolation level in effect while the transaction
runs. I guess that's a typical Hibernate advertisement.

Hope this helps

- Michael

Zarar Siddiqi schrieb:
> Michael/James,
>
> Is em.flush() the same as committing something?  I'm a little confused,
> I want to insert 100 records to a table but want to do it 10 at a time,
> so I want to call em.persist() 10 times and then call em.flush() after,
> all in the same method.  I'm in Glassfish and em.flush() doesn't commit
> anything and all rows are committed at the end of method execution.
>
> I tried setting em.setFlushMode (FlushModeType.COMMIT) but that had no
> impact.
>
> How do I go about doing this?  I'm developing an application in Seam and
> they're saying to use Hibernate to take advantage of its  "manual flush"
> implementation which no other provider has.  Is that really true?
>
> I find it hard to believe that such a core feature is missing.
>
> Thanks,
> Zarar
>
>
>
> On Fri, Jan 15, 2010 at 8:39 AM, christopher delahunt
> <christopher.delahunt@xxxxxxxxxx
> <mailto:christopher.delahunt@xxxxxxxxxx>> wrote:
>
>     Hello Michael,
>
>     The "eclipselink.flush" property is a query hint.  You will need to
>     use "eclipselink.persistence-context.flush-mode" to set the property
>     in persistence.xml.
>
>
>     Best Regards
>     Chris
>
>     Michael Simons wrote:
>
>         Hello James,
>
>         The eclipselink.flush option does not work like expected.
>         In the persistence.xml, I specified:
>              <property name="eclipselink.flush" value="Commit" />
>         But this seems to be ignored, because I get the following
>         exception, on a query before anything
>         was committed:
>
>         SCHWERWIEGEND: Exception [EclipseLink-4002] (Eclipse Persistence
>         Services -
>         1.2.0.v20091016-r5565):
>         org.eclipse.persistence.exceptions.DatabaseException
>         Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data
>         truncation: Column set to default
>         value; NULL supplied to NOT NULL column 'tour_id' at row 1
>         Error Code: 0
>         Call: UPDATE tour_position SET tour_id = ?, jdo_version = ?
>         WHERE ((tour_position_id = ?) AND
>         (jdo_version = ?))
>                bind => [null, 7, 96117, 6]
>         Query: UpdateObjectQuery(net.uniopt.domain.ot.OrderStop@17775)
>         javax.persistence.PersistenceException: Exception
>         [EclipseLink-4002] (Eclipse Persistence
>         Services - 1.2.0.v20091016-r5565):
>         org.eclipse.persistence.exceptions.DatabaseException
>         Internal Exception: com.mysql.jdbc.MysqlDataTruncation: Data
>         truncation: Column set to default
>         value; NULL supplied to NOT NULL column 'tour_id' at row 1
>         Error Code: 0
>         Call: UPDATE tour_position SET tour_id = ?, jdo_version = ?
>         WHERE ((tour_position_id = ?) AND
>         (jdo_version = ?))
>                bind => [null, 7, 96117, 6]
>         Query: UpdateObjectQuery(net.uniopt.domain.ot.OrderStop@17775)
>                at
>         org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:612)
>                at
>         org.eclipse.persistence.internal.jpa.EJBQueryImpl.performPreQueryFlush(EJBQueryImpl.java:1153)
>                at
>         org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:371)
>                at
>         org.eclipse.persistence.internal.jpa.EJBQueryImpl.getResultList(EJBQueryImpl.java:636)
>                at
>         de.optitool.biz.tp.StockManager.queryFirstAndRecentDetection(StockManager.java:786)
>                at
>         de.optitool.biz.tp.StockManager.retrieveRemainingContingentStock(StockManager.java:359)
>                at
>         de.optitool.biz.tp.StockManager.tell(StockManager.java:344)
>                at
>         de.optitool.biz.tp.ReservationManager.bookOnAbstractCont(ReservationManager.java:1064)
>                at
>         de.optitool.biz.tp.ReservationManager.bookOnCheapestProduct(ReservationManager.java:1318)
>                at
>         de.optitool.biz.tp.ReservationManager.reserveStocks(ReservationManager.java:542)
>                at
>         de.optitool.biz.tp.ReservationManager.reserveStocks(ReservationManager.java:466)
>                at
>         de.optitool.biz.tp.ResAllocController.processTour(ResAllocController.java:217)
>                at
>         de.optitool.biz.tp.ResAllocController.runTask(ResAllocController.java:141)
>                at
>         de.optitool.biz.tp.ResAllocController.runOnEntities(ResAllocController.java:78)
>                at
>         de.optitool.biz.tp.TourModificationManager.runControllers(TourModificationManager.java:595)
>                at
>         net.uniopt.ot.action.UnplanOrders.runOnEntities(UnplanOrders.java:70)
>                at
>         de.optitool.action.UserRestrictedAction.run(UserRestrictedAction.java:134)
>                at net.uniopt.ot.action.TaskAction.run(TaskAction.java:124)
>                at
>         org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
>                at
>         org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:583)
>                at
>         org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:500)
>                at
>         org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
>                at
>         org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
>                at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
>                at
>         org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3823)
>                at
>         org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3422)
>                at
>         org.eclipse.jface.window.Window.runEventLoop(Window.java:825)
>                at org.eclipse.jface.window.Window.open(Window.java:801)
>                at net.uniopt.ot.gui.MainWindow.open(MainWindow.java:566)
>                at
>         net.uniopt.app.Application.processEventLoop(Application.java:514)
>                at net.uniopt.app.Application.run(Application.java:214)
>                at net.uniopt.ot.Main.main(Main.java:49)
>
>         Any hints what might be wrong here are appreciated.
>
>         - Michael
>
>         James Sutherland schrieb:
>
>
>             Do you want to ignore the cache, or not do the flush?
>
>             To disable flushing, you can either set the EntityManager
>             flushMode, or
>             persistence unit property or use the query hint,
>
>             "eclipselink.flush"="Commit"
>
>             This will prevent to auto flush.
>
>             If you truly wish to ignore the cache, and build "detached"
>             objects directly
>             from the database, you can use,
>
>             "eclipselink.maintain-cache"="false"
>
>             The hint, QueryHints.CACHE_USAGE,
>             CacheUsage.DoNotCheckCache, just means
>             that a query will not check the cache before accessing the
>             database, the
>             query will still flush, and will still use the cache and
>             persistence context
>             when building the objects from the database result.
>
>
>             Michael Simons wrote:
>
>
>                 Hello,
>
>                 How can I tell EL to ignore the cache when executing a
>                 query, so there's
>                 no flush done before
>                 executing the query?
>                 Is this done by query.addHint(QueryHints.CACHE_USAGE,
>                 CacheUsage.DoNotCheckCache) ?
>
>                 - michael
>
>
>
>
>             -----
>             http://wiki.eclipse.org/User:James.sutherland.oracle.com
>             James Sutherland http://www.eclipse.org/eclipselink/
>              EclipseLink ,
>              http://www.oracle.com/technology/products/ias/toplink/
>             TopLink Wiki:  http://wiki.eclipse.org/EclipseLink
>             EclipseLink , http://wiki.oracle.com/page/TopLink TopLink
>             Forums:
>              http://forums.oracle.com/forums/forum.jspa?forumID=48
>             TopLink , http://www.nabble.com/EclipseLink-f26430.html
>             EclipseLink Book:
>              http://en.wikibooks.org/wiki/Java_Persistence Java
>             Persistence
>
>
>
>
>     _______________________________________________
>     eclipselink-users mailing list
>     eclipselink-users@xxxxxxxxxxx <mailto:eclipselink-users@xxxxxxxxxxx>
>     https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>
>
>
> --
> Zarar Siddiqi
> 416-737-9024
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users

--
mit freundlichen Grüßen,
Michael Simons

OPTITOOL GmbH
Bruderwöhrdstr. 15b
93055 Regensburg
Tel: +49(0)941.595780
Fax: +49(0)941.595787
_______________________________________________



--
Zarar Siddiqi
416-737-9024

Back to the top