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&connectTimeout=5000&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.
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.
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.
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.
I did it for an Oracle non-pooled connection like this:
<Resource
name="jdbc/aqds"
auth="Container"
type="oracle.jdbc.pool.OracleDataSource"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@localhost:1521:XE"
user="MYUSER"
password="MYPASSWORD" />
来源:https://stackoverflow.com/questions/1229839/how-to-create-data-source-without-pooling-in-tomcat