second JNDI connection with Oracle DBCP (UCP) in web context

123 views
Skip to first unread message

Andreas Niemeyer

unread,
Sep 24, 2012, 3:05:45 PM9/24/12
to mybatis-user
Hi,

I've tried to implement an second connection on same JNDI resource in
parallel to an given connection pool configuration without success.

I got an exception from the UCP driver that the connection pool is
already in use. I'm not allowed to change anything in the server.xml
in tomcat environment where the connection resource is setup.

The given connection pool configuration within the code looks like:

mgr =
UniversalConnectionPoolManagerImpl.getUniversalConnectionPoolManager();
pds = (oracle.ucp.jdbc.PoolDataSource)
envContext.lookup(dataSource);
mgr.createConnectionPool((UniversalConnectionPoolAdapter) pds);
mgr.startConnectionPool(POOLNAME);
mgr.setMetricUpdateInterval(60);
mgr.startMetricsCollection(POOLNAME);

The POOLNAME constant reflects the property `connectionPoolName' of
the resource configuration.

It seems to make sense how it behalf with the exception and the UCP
driver keeps the pool as registered resource, therefore a second JNDI
connection with MyBatis wouldn't be allowed.

I'm not motivated to replace the complete UCP stuff due the legacy
implementation is needed as I want only extend for new queries.

Maybe it should addressed in a OTN group, but hopefully it might
possible to cut the corner with a simple question here:

Is it possible to initialize the MyBatis configuration with a given
sql connection object (which I already have from the UCP connection
pool) ?


Kind regards,
Andreas

-

Guy Rouillier

unread,
Sep 24, 2012, 5:53:06 PM9/24/12
to mybati...@googlegroups.com
Not clear on what you are attempting to accomplish. However, if you
want to configure MyBatis to use a defined JNDI pool for one set of
statements, and then a non-JNDI pool for another, take a look at the
POOLED datasource element described in the MyBatis documentation. You'd
have to create two different session factories, one for each connection
pool.

--
Guy Rouillier

Andreas Niemeyer

unread,
Sep 25, 2012, 3:32:58 AM9/25/12
to mybatis-user
I will give it a try...

I mean if there is a way to pass a given DB connection object from
somewhere else to initialize the MyBatis stuff only with that single
connection and without any pooling capabilities.

Andreas Niemeyer

unread,
Oct 1, 2012, 1:59:46 PM10/1/12
to mybatis-user
Hi Guy,

it didn't help me, I have only access to a connection handle within
the application, not the connection data themselves to do a regular
setup as required. Also modifications on setup related files for a
JNDI data source in the server / application context is not allowed to
me.

Due the existence of a connection handle object from a "foreign" setup
- out of any other mybatis setup, it would be great to get up MyBatis
maps running on a SessionFactory with a "special" initialisation.

Does the framework provide this needs?

Many thanks in advance...

Kind regards,
Andreas

On 24 Sep., 23:53, Guy Rouillier <guy.rouill...@gmail.com> wrote:

Guy Rouillier

unread,
Oct 4, 2012, 6:25:12 PM10/4/12
to mybati...@googlegroups.com
On 10/1/2012 1:59 PM, Andreas Niemeyer wrote:
> Hi Guy,
>
> it didn't help me, I have only access to a connection handle within
> the application, not the connection data themselves to do a regular
> setup as required. Also modifications on setup related files for a
> JNDI data source in the server / application context is not allowed to
> me.
>
> Due the existence of a connection handle object from a "foreign" setup
> - out of any other mybatis setup, it would be great to get up MyBatis
> maps running on a SessionFactory with a "special" initialisation.
>
> Does the framework provide this needs?

I haven't tried this, but both SqlSessionFactory.openSession() and
SqlSessionManager.openSession() have an implementation that takes a
java.sql.Connection object as a parameter. So, theoretically, you can
use that and provide your own Connection object. Let us know how that
works.

--
Guy Rouillier

Andreas Niemeyer

unread,
Oct 7, 2012, 9:31:31 AM10/7/12
to mybatis-user
Hi,

it works, great. I haven't seen the method yet ...

I let any connection info in the config out and pass the Connection
object, here is an example.

Kind regards,
Andreas


public class MyBatisDAO {

//
------------------------------------------------------------------------------------------------
\\

private static Logger pLogger = Logger.getLogger(MyBatisDAO.class);
private static MyBatisInterface pMyBatisMapper = null;
private static SqlSession pSession = null;
//
------------------------------------------------------------------------------------------------
\\

public MyBatisDAO(Connection aCon) throws IOException {
initSQLMap(aCon);
}

//
------------------------------------------------------------------------------------------------
\\

private void initSQLMap(Connection aCon) throws IOException {

String vResource = "x/y/db/mybatis/mybatis_config.xml";
Reader vReader = null;
Properties vProps = new Properties();

try {
vReader = Resources.getResourceAsReader(vResource);
SqlSessionFactory vSQLSessionFactory = new
SqlSessionFactoryBuilder().build(vReader, vProps);
pSession = vSQLSessionFactory.openSession(aCon);
pMyBatisMapper = pSession.getMapper(MyBatisInterface.class);
pLogger.info("init mybatis mapper");
} catch (IOException e) {
throw new IOException(e.getMessage());
}
}

//
------------------------------------------------------------------------------------------------
\\

public static SimpleObject getDBDateInFutureDays(int aDaysFuture)
{
return pMyBatisMapper.getSysDate(aDaysFuture);
}
//
------------------------------------------------------------------------------------------------
\\

public static void commit() {
pSession.commit();
}

//
------------------------------------------------------------------------------------------------
\\

}
Reply all
Reply to author
Forward
0 new messages