You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Sudheer <su...@gmail.com> on 2010/04/06 12:17:03 UTC

Handling multiple databases using Cayenne+Spring

Hi,

Our existing implementation uses Spring. Application creates a Connection
object for each login user and keeps the object in session unitl user
logoff/session timeout. Now we are plugging part of implementation with
Cayenne. 

If we create DataContext for user session, does each user session have two
Connection objects?

Is there a way to use existing Connection object while creating a
DataContext?

Any sample Cayenne code detailing access to multiple databases (E.g. Oracle,
SQL Server) that uses same schema? (on spring+cayenne)

Thank You,
Sudheer

-- 
View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p699967.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: Handling multiple databases using Cayenne+Spring

Posted by Mike Kienenberger <mk...@gmail.com>.
If you write a custom DataSourceFactory and DataSource, you can have
your DataSource dynamically choose which connection information to
provide based on the current user.

On Thu, Apr 8, 2010 at 11:41 PM, Sudheer <su...@gmail.com> wrote:
>
> Hi Andrus,
>
> Thank you for the respone
>
> There is little catch in our existing design. Here is the design
>
> My application interacts with it Manger applications (could be more than
> one) that are running either on SQL server or Oracle. Schema is same for
> both the vendors. Manager application actually holds information about it's
> database connection.
>
> User who is using my application will be one Manager at a time. So, when
> user login to my application, system connects to the manager he is
> intereted, bring all connection details and keep them in HTTPSession. For
> Cayenne, I would like to keep DataContext in HTTPSession so that it will be
> user specific.
>
> I could not understand your previous response. After setting DataSource on a
> node and keeping DataContext in HttpSession affects all other users as well?
>
> What happens on below scenario?
> Using my application User A can connecto Manager A that is running on SQL
> Server Database.
> Using my application User B can connect to Manager B that is running on
> Oracle.
>
> If possible can I have sample code or examples for Spring+Cayenne
> integration. Unfortunately I am new to Spring as well :(
>
> I hope you understand my text :)
>
> Thank You
> Sudheer
>
>
>
>
> --
> View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p707573.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>

Re: Handling multiple databases using Cayenne+Spring

Posted by Sudheer <su...@gmail.com>.
Hi Andrus,

Thank you for the respone

There is little catch in our existing design. Here is the design

My application interacts with it Manger applications (could be more than
one) that are running either on SQL server or Oracle. Schema is same for
both the vendors. Manager application actually holds information about it's
database connection.

User who is using my application will be one Manager at a time. So, when
user login to my application, system connects to the manager he is
intereted, bring all connection details and keep them in HTTPSession. For
Cayenne, I would like to keep DataContext in HTTPSession so that it will be
user specific.

I could not understand your previous response. After setting DataSource on a
node and keeping DataContext in HttpSession affects all other users as well?

What happens on below scenario? 
Using my application User A can connecto Manager A that is running on SQL
Server Database.
Using my application User B can connect to Manager B that is running on
Oracle.

If possible can I have sample code or examples for Spring+Cayenne
integration. Unfortunately I am new to Spring as well :(

I hope you understand my text :)

Thank You
Sudheer




-- 
View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p707573.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: Handling multiple databases using Cayenne+Spring

Posted by Andrus Adamchik <an...@objectstyle.org>.
I should add that DataNode is a shared object, so  
DataNode.setDataSource(..) will affect all threads accessing Cayenne.  
So if you have thread-specific DataSource (something your original  
message seems to imply), it won't be appropriate to set it globally.  
This is another reason why a custom factory may be a better idea, as  
it can integrate with Spring backend, taking into account all its  
configuration details. So here is another idea:

http://cayenne.apache.org/doc30/api/org/apache/cayenne/conf/Configuration.html#setDataSourceFactory%28org.apache.cayenne.conf.DataSourceFactory%29

The difference between DataSourceFactory set in the Modeler and the  
one set via Configuration is that the later can itself originate from  
Spring, and get all the stuff it needs via injection. Don't know  
enough about Spring to give you more details, but if you can inject  
web-layer objects (request, session) into Spring beans, you should be  
able to pull it off.

Otherwise your best bet is still a custom DataSourceFactory, but used  
with a static ThreadLocal that stores current HttpServletRequest.

Andrus


On Apr 8, 2010, at 4:40 PM, Andrus Adamchik wrote:

> You can.
>
> On Apr 8, 2010, at 4:26 PM, Sudheer wrote:
>
>>
>> Can we implement without customizing DataSourceFactory? Because  
>> entire
>> connection related information is in an object that is part of  
>> HTTPSession.
>> getDataSource method only takes String parameter.
>
> A factory can be smart about that, getting this info from a thread  
> local or something.
>
>> Can I have empty xxxx.driver.xml configured and attach DataSource
>> dynamically to the node?
>
> You can use DataNode.setDataSource(..). You may still want create a  
> custom factory that does nothing to make sure Cayenne starts without  
> errors.
>
> Andrus
>


Re: Handling multiple databases using Cayenne+Spring

Posted by Andrus Adamchik <an...@objectstyle.org>.
You can.

On Apr 8, 2010, at 4:26 PM, Sudheer wrote:

>
> Can we implement without customizing DataSourceFactory? Because entire
> connection related information is in an object that is part of  
> HTTPSession.
> getDataSource method only takes String parameter.

A factory can be smart about that, getting this info from a thread  
local or something.

> Can I have empty xxxx.driver.xml configured and attach DataSource
> dynamically to the node?

You can use DataNode.setDataSource(..). You may still want create a  
custom factory that does nothing to make sure Cayenne starts without  
errors.

Andrus

Re: Handling multiple databases using Cayenne+Spring

Posted by Sudheer <su...@gmail.com>.
Can we implement without customizing DataSourceFactory? Because entire
connection related information is in an object that is part of HTTPSession.
getDataSource method only takes String parameter.

Can I have empty xxxx.driver.xml configured and attach DataSource
dynamically to the node?

Thank You,
Sudheer

-- 
View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p705924.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: Handling multiple databases using Cayenne+Spring

Posted by Andrus Adamchik <an...@objectstyle.org>.
There are a few samples in Cayenne itself. E.g.:

https://svn.apache.org/repos/asf/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/JNDIDataSourceFactory.java

Or a more complex one, parsing XML file:

https://svn.apache.org/repos/asf/cayenne/main/branches/STABLE-3.0/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/conf/DriverDataSourceFactory.java

"Location" is a bit of a misnomer (and will be renamed in the new  
versions of Cayenne). This is really an arbitrary String understood by  
the factory and meaningless to Cayenne, that is stored in cayenne.xml  
and passed by Cayenne to the factory 'getDataSource' method. So e.g.  
you can use that parameter to give your factory a hint on what  
platform you are on or something.

Hopes that clarifies it a bit.

Andrus

On Apr 7, 2010, at 11:31 AM, Sudheer wrote:

>
> Thank you for the response.
>
> Can I have sample code for this? or reference document explaining  
> how to do.
>
> Actually I am coonfused with the approach. Within modeler, for the  
> custom
> data source factory, location is mandatory. I would like to know how
> cayenne.xml looks like while customizing data source factory?
>
> Can we create multiple domains in Cayenne modeler to support different
> platforms? or within single domain do we need to create multiple  
> nodes?
>
> Thank You,
> Sudheer
> -- 
> View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p702422.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>


Re: Handling multiple databases using Cayenne+Spring

Posted by Sudheer <su...@gmail.com>.
Thank you for the response.

Can I have sample code for this? or reference document explaining how to do.

Actually I am coonfused with the approach. Within modeler, for the custom
data source factory, location is mandatory. I would like to know how
cayenne.xml looks like while customizing data source factory?

Can we create multiple domains in Cayenne modeler to support different
platforms? or within single domain do we need to create multiple nodes?

Thank You,
Sudheer
-- 
View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p702422.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Re: Handling multiple databases using Cayenne+Spring

Posted by Andrus Adamchik <an...@objectstyle.org>.
DataContext itself is "disconnected" (i.e. it doesn't hold an open  
connection all the time). It obtains a JDBC connection temporarily as  
needed via Cayenne stack for the duration of a query or a commit. The  
Connection is checked out from a DataSource attached to the DataNode.  
So you can create a DataSource implementation bound to your Spring  
container. To do this, you need to implement DataSourceFactory  
interface:

http://cayenne.apache.org/doc30/api/org/apache/cayenne/conf/DataSourceFactory.html

and type the name of your implementation in the Modeler for the  
project DataNode "DataSource Factory" field.

Andrus


On Apr 6, 2010, at 1:17 PM, Sudheer wrote:

>
> Hi,
>
> Our existing implementation uses Spring. Application creates a  
> Connection
> object for each login user and keeps the object in session unitl user
> logoff/session timeout. Now we are plugging part of implementation  
> with
> Cayenne.
>
> If we create DataContext for user session, does each user session  
> have two
> Connection objects?
>
> Is there a way to use existing Connection object while creating a
> DataContext?
>
> Any sample Cayenne code detailing access to multiple databases (E.g.  
> Oracle,
> SQL Server) that uses same schema? (on spring+cayenne)
>
> Thank You,
> Sudheer
>
> -- 
> View this message in context: http://n3.nabble.com/Handling-multiple-databases-using-Cayenne-Spring-tp699967p699967.html
> Sent from the Cayenne - User mailing list archive at Nabble.com.
>