This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

New data in transactions

Jun 14th, 2012, 01:51 AM

I never remember when I have to call save() and when I can just let the commit of the transaction do all my work for me.

So I have an Event which is already a node in the db. I want to add an ItemNeededForEvent. This also might require creating a brand new Item object. So the ItemNeededForEvent has an Event reference and an Item reference. When I find that the item is new, I create a new Item object (item) and create a new ItemNeededForEvent (itemNeeded) object. I set item in itemNeeded, then add itemNeeded to a collection in the Event object. I never call save. Should I be calling save on the new Item, on the new ItemNeededForEvent, or will it all be handled when I commit.

Code in my EventServiceImpl class, all the methods in the public interface are all @Transactional.

Comment

Yes, using advanced. Yes, I have tests and they pass. And in the app, it doesn't exactly work as advertised. Or it is sporadic. Sometimes it saves, sometimes it doesn't.

I think also the type of node that it is might also make a difference. Like a vertex node which has relation to three nodes, basically there just to hook the three nodes on one relationship, so to speak. If you create a new vertex and set all three relation nodes and add it to one of those three nodes collections, all within a transaction, that new vertex node is still not saved.

So here is where I was getting intermittent saves with this code, which is not a vertex node. This is a Service method that is transactional. I put @Transactional on the class level, and yes the method is in the interface.

As you can see I have to call save on event, otherwise the itemNeeded is not saved. And notice that I also have code that might create a new Item node, and if it does, I have to call itemRepository.save(item) in order for it to be saved and used in itemNeeded.

The event.addItemNeededForEvent, the Event node is retrieved from a repository, so advanced should proxy that and when I call addItemNeededForEvent that method in the node creates another node