Java connection pool without JNDI?

I have a connection pool for accessing a MySQL database from a servlet. I get the data source using JNDI, which is defined in my META-INF / context.xml file.

Everything works fine, but I have to place the JAR driver JAR in the Tomcat / common / lib folder, not the webapp WEB-INF / lib; otherwise, JNDI will not work (ClassNotFoundException: com.mysql.jdbc.Driver).

Is there any other way to get a data source that allows me to put a JAR in WEB-INF / lib? All the examples I found over the Internet use JNDI ...

(This is a fairly simple application, I would prefer not to import 53 JARs of some frameworks to solve my problem :)

Thanks!

+3
source share
5 answers

Although most of these answers are about pools, I don’t think your question is?

I think the most direct answer to your question is to simply import and use the DataSource implementation provided by your driver. Are you using MySQL Connector / J? then MysqlDataSource .

There are methods for setting a username, password, etc.

If you do not need it in JNDI, you do not need to configure it in JNDI via Tomcat. Of course, you can store the driver pitcher in WEB-INF / lib. If you want to unite around this DataSource, just use Commons DBCP and Pool.

Here's an example of creating your own DataSource union from a DataSource.

+3
source

, :

+1

JNDI, Tomcat, , WAR . , .

, Apache Commons DBCP (, , Apache Commons Pool).

+1

. , , .

, ... , , ! -.

Java EE, JDBC, , . . , .

, .

+1

, context.xml. , . . . , ,

  • < s > . JAXP, Apache Digester. , Apache Commons.

  • <Resource> - -. .

  • ,

    DataSource ds = org.apache.commons.dbcp.BasicDataSourceFactory.createDataSource(prop);

, JNDI (useNaming = "false" ), .

0
source

Source: https://habr.com/ru/post/1720264/


All Articles