Re: Spring + Cayenne

From: Chris Poulsen (chri..esluop.dk)
Date: Thu Apr 29 2010 - 17:26:36 EDT

  • Next message: Victor Leung: "Re: Large number of EventManager threads causing OutOfMemoryException"

    Hi Mike,

    On 29-04-2010 20:03, MGargan..scholar.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
    



    This archive was generated by hypermail 2.0.0 : Thu Apr 29 2010 - 22:04:14 EDT