Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple datasources created when using Spring JNDI template #5

Closed
sbearcsiro opened this issue Aug 28, 2017 · 4 comments
Closed

Multiple datasources created when using Spring JNDI template #5

sbearcsiro opened this issue Aug 28, 2017 · 4 comments

Comments

@sbearcsiro
Copy link

I'm using Simple JNDI with a third party library that uses a Spring JndiTemplate to lookup objects from JNDI. The Spring JndiTemplate runs code like this for any JNDI operation (eg a lookup would execute a JndiCallback that ran ctx.lookup) an object:

    public <T> T execute(JndiCallback<T> contextCallback) throws NamingException {
        Context ctx = getContext();
        try {
            return contextCallback.doInContext(ctx);
        }
        finally {
            releaseContext(ctx);
        }
    }

This is problematic as I have ~5 lookups for what is supposed to be the same connection pool and Spring causes a new Database Connection Pool to be created each time a lookup is run.

Would you approve of adding another property to disable removing contexts on close?

@h-thurow
Copy link
Owner

I am working on a new version, coming soon, that will address this problem.

@h-thurow
Copy link
Owner

h-thurow commented Sep 2, 2017

Done. See org.osjava.sj.jndi.ignoreClose

@h-thurow h-thurow closed this as completed Sep 4, 2017
@sbearcsiro
Copy link
Author

@h-thurow Thanks for the change!

@mauromol
Copy link

mauromol commented Sep 6, 2024

Hello,
beware that this advertised code:

Hashtable env = new InitialContext().getEnvironment();
env.remove("org.osjava.sj.jndi.ignoreClose");
env.put("java.naming.factory.initial", "org.osjava.sj.SimpleJndiContextFactory");
new InitialContext(env).close();

to actually close the context when the ignoreClose is set, does not really work. Apart from the fact that it uses an unchecked Hashtable (since the one returned by getEnvironment() is otherwise a Hashtable<?,?>, on which you cannot put), the main problem is that, on the second call to the contractor of InitialContext, even if you pass an env without the mentioned property, that constructor will call javax.naming.InitialContext#init, which saves into a variable named myProps the result of calling ResourceManager.getInitialEnvironment(environment) with the passed in environment. This call seems to merge the previous environment with the new one, so after this the mentioned myProps (which is subsequently used to initialize the context) still has org.osjava.sj.jndi.ignoreClose set to true.

I don't have any elegant solution for the unchecked access problem. For the second one, instead, the solution I found is to set org.osjava.sj.jndi.ignoreClose to false, instead of removing it from the map:

@SuppressWarnings("unchecked")
Hashtable<Object, Object> env = (Hashtable<Object, Object>) new InitialContext().getEnvironment();
env.put("org.osjava.sj.jndi.ignoreClose", "false");
env.put("java.naming.factory.initial", "org.osjava.sj.SimpleJndiContextFactory");
new InitialContext(env).close();

In this way I was finally able to actually invalidate my context in an "aftereach" method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants