This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

I need to create a session factory for user-specified (and non-spring managed) database connections. These connections are used to create unique session factories for hibernate. Associated with each session factory are layers of DAOs and services that depend on the session factory. The session factory is created from a user-specified connection parameters and uses a long-conversation hibernate session to manage performance. The DAOs and services have different context xml files defining them. There is a global application context defined as well for the entire application that defines some other app-specific singletons.

My application can connect to multiple "project" databases and users can add a new database connection on their own to interact with the project data in it. Each project should contain its own hibernate cache and use long conversations. I know how to do the hibernate part but all the examples I see around spring always define database connections in xml files or property files. For me, the connection information is non-spring managed and is dynamically defined by the user.

For the application I want to create a context for each "project." Each project will have its own session factory and supporting DAO and service objects.

[Global app context - same no matter which project I connect to]
===>[project #1 define my "hibernate session" in a context]
=======>[DAO and service context that references "hibernate session"]
===>[project #2 define another unique "hibernate session" in a context]
=======>[DAO and service context that references "hibernate session"]

Since my DAOs and service context need to use that "hibernate session" and that hibernate session is dependent on the connection info which is managed outside of spring, how do I define my contexts so I can programmatically create a context when a "project" is opened by the user but use non-spring POJOs to manage the connection info?

I have seen the documentation and posts on defining hierarchical contexts and the use of SingletonBeanFactoryLocator. This all seems doable. However, I need to programmatically define a context that sits between the shared global context and one specific that I want to instantiate and depends on my programmatically defined context holding the "hibernate session." The DAO and service context has a "hibernateSession" bean that I have defined and that is referenced by id for setter injection where needed on those DAOs and services.

b. When you need to create the "project" context, create this programmatically as well but specify the toplevel context as the parent. The application context configuration files I have are Connections.xml and ServiceObjects.xml. The Connections xml file has a data source and the session factory bean definitions. The ServiceObjects xml file has all the DAO and service objects that depend on the Connections beans.

c. After creating the new "project" context, access the data source bean. Configure it with the data source properties that are managed by the non-spring portions of my application.

However, after doing (b) I get an exception because the Connection bean is not fully configured programmatically before it tries to create the session factory. Even with lazy-init on the session factory, there are DAO dependencies which are not lazy (and I should not assume that everything can be marked as lazy). Hence, I need to configure the data source prior to creating the context but that's a chicken-and-egg problem.

Thoughts on how to do this cleanly using as minimal non-standard design steps as possible?

Note I could also use the SingletonBeanFactoryLocator class as well to set this up more spring-like, but the chicken-and-egg problem would still exist.

Comment

I did find a simple solution to this. The DataSource object needs to be defined one level up in the hierarchy in order to already exist prior to creating the lower levels of the hierarchy. This way, the process is

a) Create the toplevel context.. at the start of the app.

b) Grab the datasource from the toplevel, configure it.

c) Then create the hierarchy using toplevel as the pre-exsiting parent context.

I could even use a SingletoBeanFactoryLocater object but there is one catch...how do I reference the current context being created so I can set the parent constructor argument when I specify the ClassPathXMLApplicationContext bean in the context? Is there a "self" attribute?

Comment

Then use the parent context along with a layer of bean definitions with the data source and then another layer of the context with the DAOs and create all of this together (with the top-most context already created). The data source is configured in code right after the creation and before any access to the session factory bean is made.