[Neo] NotInTransactionException?

Mattias Ask mattias.ask at jayway.se
Fri May 22 23:35:00 CEST 2009

Ok, I hope you're wondering why I did this mistake... Well, the answer  
is in the cool thing that I was trying out.

I really dislike for my domain objects to know of my NeoService  
instance since I see my domain objects as Nodes (see http://blog.jayway.com/2008/10/06/neo4j-matches-my-mental-model-of-information/) 
  and I don't want my Nodes to know of their container, the NeoService.

If I try to do something more DDD I get in trouble because of this.  
Take a Twitter clone: I create a User and that User should be able to  
create a Tweet. When I call UserNode.createTweet(String) I need to  
create a Node to put in my TweetNode... How do I do that without  
injecting the NeoService into every UserNode I create?

The answer came up with lays in the load-time-weaving feature  
introduced in Spring 2.5... To make some magic I only have to do the  
1. Enable load-time-weaving in my spring context.
2. Annotate the TweetNode-class with @Configurable
3. Create an default constructer that doesn't take a Node in  
TweetNode, create a setUnderlyingNode(Node) and annotate the setter  
with @Autowired
4. Create a factory bean for Nodes that which is scope="prototype" in  
the context.
5. Create a prototype bean of TweetNode and set the Node from the  
factory bean in that.

What does this result in? It results in the fact that every time I do  
"new TweetNode();" in the code, the new object gets a underlying node  
magically injected to the setUnderlyingNode(Node). This way none of my  
DomainNode-objects need to know of my NeoService, I can new them up as  
I want (as long as they are prototyped in the context and annotated in  
the code as needed) and I can still rely on them having an underlying  
node even if you new them up without having access to the  
NeoService... And I think that is pretty cool :)

I hope this gives my prior mistake some context (no pun intended ;)

I'll write a blog post on this at http://blog.jayway.com when I get  
the time, but for now it's weekend! :)

Best regards, Mattias Ask
Jayway AB, +46 701 469284

Jayway is the founder of Öredev and Qi4J

On 22 maj 2009, at 19.49, Anders Nawroth wrote:

> Emil Eifrem skrev:
>> I think the problem is that you haven't given Spring a chance to do
>> its magic. Spring must inject its transaction handling somehow
>> (dynamic proxies, load-time weaving, etc)
> I think what you need to do is to let Spring instantiate any class  
> where
> you use transactions.
> You can look at how it's done in the IMDB app:
> http://wiki.neo4j.org/content/IMDB_Web_application
> /anders
> -- 
> Anders Nawroth [anders at neotechnology.com]
> GTalk, Skype: anders.nawroth
> Phone: +46 737 894 163
> http://twitter.com/nawroth
> http://blog.nawroth.com/
> _______________________________________________
> Neo mailing list
> User at lists.neo4j.org
> https://lists.neo4j.org/mailman/listinfo/user

More information about the User mailing list