You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by MG...@escholar.com on 2010/04/29 20:03:22 UTC

Spring + Cayenne

Is anyone else configuring their data sources through Spring?  How does 
one go about doing this?  I was trying something, but it just keeps 
picking up the driver.xml file that cayenne modeler created.  Plus I keep 
getting this nifty MalformedObjectNameException complaining about a 
nonprintable invalid character in value part of property.  Not even sure 
what file that's happening in cayenne modeler doesn't complain and I don't 
see anything strange when looking through the xml.

Hrmph....

-Mike

Re: Spring + Cayenne

Posted by Chris Poulsen <ch...@nesluop.dk>.
Hi Mike,

On 29-04-2010 20:03, MGargano@escholar.com wrote:
> Is anyone else configuring their data sources through Spring?  How does
> one go about doing this?  I was trying something, but it just keeps
> picking up the driver.xml file that cayenne modeler created.  Plus I keep
> getting this nifty MalformedObjectNameException complaining about a
> nonprintable invalid character in value part of property.  Not even sure
> what file that's happening in cayenne modeler doesn't complain and I don't
> see anything strange when looking through the xml.
>
> Hrmph....
>
> -Mike

I ran into the need to use a datasource from spring the other day. 
Here's how I managed to get things mocked together:

1) I created my own Cayenne DataSourceFactory that just holds a 
reference to the spring-injected dataSource.

2) I'm binding the DataContext to a ThreadLocal in a filter (this being 
a web-app). The filter receives the dataSourceFactory from spring and 
initializes Cayenne (v2) using the following code snippet (a spring 
callback):

public void afterPropertiesSet() throws Exception {
     Configuration conf = new DefaultConfiguration();
     conf.setDataSourceFactory(dataSourceFactory);
     Configuration.initializeSharedConfiguration(conf);
     conf.setDataSourceFactory(dataSourceFactory);
}

After this code has been executed I can create a DataContext using:

ctx = DataContext.createDataContext();

And bind it with:

DataContext.bindThreadDataContext(ctx);

I'm not 100% sure which scope I want for the DataContext, but currently 
I'm leaning towards a "fresh" context per request as opposed to a 
context per session. Perhaps someone could chime in with their best 
practice for this one?

Also if you want to let spring manage transactions and have cayenne and 
user code (jdbc template or raw jdbc access for instance) to be able to 
work together (for instance adding something in cayenne and linking 
something against the newly inserted row using spring jdbc code you need 
to wrap the datasource passed to caynne in a 
TransactionAwareDataSourceProxy (or something like that) in order to 
actually share the underlying connection in the transaction.

I hope you can use the cayenne init code I pasted, it took some fiddling 
around before I managed to find a way of initializing it without getting 
errors.

-- 
Chris





Re: Spring + Cayenne

Posted by Andrus Adamchik <an...@objectstyle.org>.
Somebody more familiar with Spring may comment on the DataSource  
details, but in Cayenne you probably need a custom DataSourcFactory  
set in the Modeler for a given DataNode, that will in turn pick up the  
DS from Spring.

Andrus

On Apr 30, 2010, at 2:03 AM, MGargano@escholar.com wrote:

> Is anyone else configuring their data sources through Spring?  How  
> does
> one go about doing this?  I was trying something, but it just keeps
> picking up the driver.xml file that cayenne modeler created.  Plus I  
> keep
> getting this nifty MalformedObjectNameException complaining about a
> nonprintable invalid character in value part of property.  Not even  
> sure
> what file that's happening in cayenne modeler doesn't complain and I  
> don't
> see anything strange when looking through the xml.
>
> Hrmph....
>
> -Mike