Transaction did not seem to be working with spring-data

1,689 views
Skip to first unread message

Newbie

unread,
Apr 5, 2012, 4:00:38 PM4/5/12
to Neo4j
I have two repository interfaces that extends from GraphRepository. In
my service layer method, I save data using both repositories. When the
second save fails, the data saved by the first repository gets
persisted in my embedded db.

I am using spring data neo4j 2.0.1 release, neo4j 1.6, and spring
3.1.0.release.

I tried to annotate my method with @Transactional,
@Neo4jTransactional, but neither worked properly. What could be the
problem? Is there documentation that I can follow step-by-step to
setup transaction control with spring-data-neo4j?

Thanks,

Newbie

unread,
Apr 8, 2012, 8:44:23 AM4/8/12
to Neo4j
Anyone has any insight?

Do I have to revert to use native api to persist data?

Peter Neubauer

unread,
Apr 8, 2012, 11:05:13 AM4/8/12
to ne...@googlegroups.com
Got the code somewhere to look at, and the failing test?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

Neo4j                                - Graphs rule.
Program or be programmed - Computer Literacy for kids.
http://foocafe.org/#CoderDojo

Michael Hunger

unread,
Apr 8, 2012, 1:09:16 PM4/8/12
to ne...@googlegroups.com
What does your spring config look like?

Does your service also have an @Transactional? I assume you want to have a tx that spsns both repo calls?

Sent from mobile device

Neil Russell

unread,
Mar 19, 2013, 11:11:52 PM3/19/13
to ne...@googlegroups.com
Hi,

I'm attaching my question to this thread because it seems so similar.

Using Neo4j 1.8.1 embedded graph database, SDN 2.1.0 in jetty container.

applicationContext.xml has:

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven />

I have a class ObjectiveComponent  that does its own auto-wire of ObjectiveReposistory class, which seems to be working fine for us.

I added @Transactional to the update() method, which does a repository.save() but then does a lot of other stuff, and I want to roll back all of it if anything else fails withing that update() method.

To test this, I throw a RuntimeException at the bottom of my method after the repository.save() but before the end of the method, but the repository.save() still commits and does not roll back.

What am I doing wrong?

I also do not find the @Neo4jTransactional annotation anywhere.

Thanks,

--N

Michael Hunger

unread,
Mar 20, 2013, 1:52:21 AM3/20/13
to ne...@googlegroups.com
The rest api only has a tx per request.

So @Transactional doesn't really work with server.

The rest-binding just uses a null-tx

Michael

Sent from mobile device
--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Neil Russell

unread,
Mar 20, 2013, 1:45:34 PM3/20/13
to ne...@googlegroups.com
Hi,

We are using RESTlet with jetty and Spring to handle the REST requests, which get sent into a helper class
that is annotated @Component (I also tried @Service). I also tried @Transactional on the ObjectiveRepository class per some notes I found elsewhere.
None of that works, no new transaction is created when the method fires.

I have so far been able to work around it by coding my own TX management in every method, very ugly but at least I can get it to work.

So there is no way to use the @Transactional to wrap a method on a jetty server and have clean code?

--N

Neil Russell

unread,
Mar 20, 2013, 1:47:01 PM3/20/13
to ne...@googlegroups.com

Michael Hunger

unread,
Mar 20, 2013, 1:48:44 PM3/20/13
to ne...@googlegroups.com
Currently real transactions are only available if you use SDN against an embedded database in a application or application server or write a server extension for Neo4j which gives you also access to an embedded graph database.

Remote transaction support will come with the binary cypher driver in 2.1 or so.

Cheers

Michael

Michael Hunger

unread,
Mar 20, 2013, 1:50:08 PM3/20/13
to ne...@googlegroups.com
But as I stated only against embedded graph databases.

Over REST it will be one transaction per request and uses a Null-Transaction-Handler in the java-rest-binding.

Michael

Neil Russell

unread,
Mar 21, 2013, 10:17:35 AM3/21/13
to ne...@googlegroups.com
Sorry if I wasn't clear - we are using an embedded database in jetty.

--N

Michael Hunger

unread,
Mar 21, 2013, 11:37:11 AM3/21/13
to ne...@googlegroups.com
I had never problems with that how do you inject your services? Any way to share a failing test?

Sent from mobile device

Neil Russell

unread,
Mar 22, 2013, 1:58:49 PM3/22/13
to ne...@googlegroups.com
basic outline

@Autowired
protected ObjectiveRepository repository;

private static ObjectiveComponent component = null;

private static ApplicationContext ctx;


@Autowired
private PlatformTransactionManager txManager;

public static ObjectiveComponent getInstance() {
if (component == null) {
component = new ObjectiveComponent();
component.init();
}
return component;
}

private void init() {
ctx.getAutowireCapableBeanFactory().autowireBean(component);
}

--N

Neil Russell

unread,
Apr 1, 2013, 11:49:01 AM4/1/13
to ne...@googlegroups.com
To be clear, annotating any methods in the below class as @Transactional, does not appear to work - no transaction is started when such methods are called.

--N

Peter Neubauer

unread,
Apr 2, 2013, 7:04:39 AM4/2/13
to Neo4j User
If that is the case, maybe your @transactional configuration in Spring
is not set up correctly? That means you should have something like

<tx:annotation-driven transaction-manager="transactionManager" />

In your configuration somewhere?

/peter

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

The authoritative book on graph databases - http://graphdatabases.com
Neo4j questions? Please use SO - http://stackoverflow.com/search?q=neo4j

Michael Hunger

unread,
Apr 2, 2013, 7:26:19 AM4/2/13
to ne...@googlegroups.com
Are you sure this works in general? After all you are creating this class with "new" and don't get it from the spring context where it would be enhanced (using a proxy or AJ) to add the transactional interceptors.

ctx.getAutowireCapableBeanFactory().autowireBean(component);

just injects dependencies imho, so @Transactional at the dependencies should work but I doubt that they work in your current class.


On Mon, Apr 1, 2013 at 5:49 PM, Neil Russell <neil.e....@gmail.com> wrote:

Neil Russell

unread,
Apr 4, 2013, 4:51:51 PM4/4/13
to ne...@googlegroups.com
Hi,

I had a chance to try a few things -

I believe you are correct that calling "autoWire" takes care of basic DI stuff, but probably not a lot of other stuff.

I changed my code to instead ask the context to getBean() for my class, and it appears that this does get the @Transactional notation working.

I found several mentions online about @Neo4jTransactional annotation, but I don't see those in SDN 2.1

( I was wrong, we are still on SDN 2.1, FWIW).

Thanks guys.

--N

Michael Hunger

unread,
Apr 5, 2013, 5:59:13 AM4/5/13
to ne...@googlegroups.com
@Neo4jTransactional is used internally and is mostly there if you have a parallel relational transaction-manager in your code and don't want to change all @Transactionals to use a qualifier.
Reply all
Reply to author
Forward
0 new messages