|
The solution with singletons is still problematic considering the classloaders.
./alex -- .w( the_mindstorm )p. The attached patch contains org.apache.jackrabbit.core.TransientRepository, a proxy repository class that automatically initializes and shuts down the underlying RepositoryImpl instance when sessions are opened or closed. I implemented this as a separate class to avoid overloading the already heavy RepositoryImpl class. The implementation is quite clean except for two things: 1) it loads the default repository descriptors directly from repository.properties, and 2) it messes with RepositoryImpl.loggedOut(SessionImpl) to avoid a nasty infinite loop in RepositoryImpl.shutdown().
The class is quite easy to use and works very well with component containers like Spring where setting up an explicit shutdown call is difficult and cumbersome. If it weren't for the repository initialization and shutdown overhead, it would also make a fine candidate for solving the deployment model 2 shutdown issues discussed lately on the mailing list. The implementation also relies on all clients properly closing all the sessions they've opened. If needed, the implementation could be modified to use a WeakHashMap to cope with lost sessions. Example code: RepositoryConfig config = RepositoryConfig.create("...", "..."); Repository repository = new TransientRepository(config); Session session = repository.login(); // Repository gets initialized try { // Use the session } finally { session.logout(); // Repository gets shut down } Any problems with this approach that I haven't noticed? I've been quite happy using a class like this in my Spring-based projects, so unless anyone objects I'll go ahead and commit it so others can use it too. Günther Humer asked:
> What about sth. like implementing the Repository as a Singleton? I think that's a solution looking for a problem. It's a different issue than the one raised here, and besides I think that guarding against duplicate repository configuration is better done at the application or container level. See also http://c2.com/cgi/wiki?SingletonGlobalProblems jukka's patch:
> Any problems with this approach that I haven't noticed? I've been quite happy using a class > like this in my Spring-based projects, so unless anyone objects I'll go ahead and commit it > so others can use it too. +1 i think that TransientRepository is a very useful alternative for certain use-cases. Committed the proposed TransientRepository class in revision 355430 with the following changes:
* Added a simple TransientRepository.RepositoryFactory interface to allow greater control over the repository initialization process * Added a two utility constructors * Added an initial test case for the TransientRepository class (more complete unit testing would require a separate test repository or setting up mock RepositoryImpl instances) |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
But not in the common term of singleton, more like one instance for each Repository-Home directory.
RepositoryImpl.getRepository(homeDir1) provides the same instance as.
RepositoryImpl.getRepository(homeDir1)
But
RepositoryImpl.getRepository(homeDir2) provides a new instance.
The repository is still shutdown, with the shutdown hook and initialized, when the user creates a Session or gets the Repository.