views:

554

answers:

3

I use JNDI context to create datasource for JDBC drivers in Tomcat's context.xml file like this,

<Resource name="db/test" 
          type="javax.sql.DataSource" 
          driverClassName="com.test.jdbc.Driver"
          url="jdbc:fastdb://localhost:3306/session_db?autoReconnect=true&amp;connectTimeout=5000&amp;socketTimeout=5000"
          zeroDateTimeBehavior="convertToNull"
          username="dbuser"
          password="password"
          maxActive="100"
          maxWait="2"
          removeAbandoned="true"
          removeAbandonedTimeout="60"
          logAbandoned="true" />

By default, Tomcat will use DBCP data source factory and created pooling data sources. The specific database and driver we use already supports pooling in lower level and the extra pooling actually hurts performance. Is there anyway to create basic datasource (without pooling) using JNDI resource like this so I can switch between different databases with minimum configuration changes?

I know I can write my own datasource factory or use the ones from other drivers to achieve this but I am looking for an easier solution.

+1  A: 

Not quite sure, if this is something that would satisfy you, but you can always use Spring JDBC support without using datasources managed by Tomcat.

Grzegorz Oledzki
We just removed Spring. We did see some performance gain and don't want go back to it.
ZZ Coder
Wow... Bad Decisions Of Our Time....
skaffman
A: 

Does the JDBC driver you are using supply an implementation of javax.naming.spi.ObjectFactory or some other connection object that you could configure using org.apache.naming.factory.BeanFactory? Either of those solutions would not require you to write custom code or add any additional 3rd party libraries.

laz
No. Nothing like that is provided in the JDBC driver.
ZZ Coder
Then I don't think there is an easier solution than writing your own factory or finding another library to provide one.
laz
A: 

If you really want just one connection, try setting the intialSize to 1 and maxActive to 1 in your DBCP configuration.

In the long term, if you don't want to use connection pooling, then don't use JNDI to configure your datasource, just create it directly in the code of your web application.

Edit:

In your comment you say: "It only allows one connection and the server hangs right away because every request is waiting for the connection."

Yes, if you effectively turn off pooling by making the pool size 1, that is what will happen. But the title of your question is "How to Create DataSource without Pooling" so I'm confused about what exactly you are trying to accomplish.

My suggestion would be to not use JNDI to define your connections (the whole purpose of defining a JNDI datasource is for connection pooling across web applications). Instead, define your connection in your servlet code for your two cases, one case that continues to use DBCP, the other case that uses your other low-level connection pool.

Michael Sharek
Tried maxActive to 1 and it doesn't work. It only allows one connection and the server hangs right away because every request is waiting for the connection.We use JNDI because that's easiest for DBCP. We use 2 different databases interchangeably. Don't really want to have 2 different ways to configure it.
ZZ Coder