views:

487

answers:

3

I use EclipseLink in my application. EclipseLink uses some connection pool. I use EclipseLink internal one. Connection pool creates connection when it is needed and then keeps it for future use.

I need to make one specific SQL call on each connection when it is created, but only once. What I need to do is to grant user specific role on oracle. For security this user has this role, but disabled, and needs to enable it.

I don't want to do it each time connection is taken from the pool, only when it is created.

How can I do it?

A: 

Firstly: what do you need to do on creating a connection? I ask because there might be a specific solution to your problem.

As for how to do this, it depends entirely on what connection pool you're using and how you've set it up. Certain connect pools will allow you to provide or define a connection factory for creating new connections. In that case you can initialize them however you want to and that's the approach I'd suggest if possible.

It's hard to answer further without more detail about your setup however.

cletus
I added details to the question.
amorfis
A: 

EclipseLink can't do this out of the box. You must create a new class that extends org.eclipse.persistence.sessions.server.ConnectionPool and override the method buildConnection(). This method will be called when a new connection is created.

Aaron Digulla
A: 

I found better solution. I'll put it here in case anybody in future looks for this.

I use my own SessionCustomizer. In which I have:

public void customize(Session session) throws Exception {
    DatabaseLogin login = session.getLogin();
    Connector connector = login.getConnector();

    login.setConnector(new ConnectorWrapper(connector, m_onCreationQuery));
}

So there is my own ConnectorWrapper, which in turn wraps original Connector and when creating Connection, uses original one to create it, then calls SQL query on it, then returns it.

amorfis