You'll find here :

http://www.5flow.com/tmp/tomcatjndidb.zip

a very small sample that works on my computer (with IntelliJ project). Just change the context.xml with your database. Viewing the home page will create a database, insert records, then display them.

The data source is of type javax.sql.datasource and this is the database connection pool. In mysql console, do : show processlist;

when reloading the home page of the sample you can see that the connection has always the same id. But if you restart tomcat the id changes. This means that tomcat keeps the connection to the db in a pool instead of creating a new connection at each ds.getConnection();


On 19/07/2017 06:03, Avinash Krishnan wrote:
Hello Riccardo,

When I try using the pool properties(Without JNDI ) it gives me URL Cannot
Be null error. What I have understood is that, when we make the data source
as a static variable or a member variable of another class and try to use
it another class's function it throws error. If I instantiate and use Data
Souce on same function it is working.

I am wondering how to use the JNDI based Tomcat JDBC Connection Pool.
If I use Context variable and instantiate DataSouce object,the object
should be of type javax.sql.datasource and we don't get the
latest  org.apache.tomcat.jdbc.pool.DataSource;.

Any idea find the exact way to implement JNDI usage of
new  org.apache.tomcat.jdbc.pool.DataSource; ?

On Tue, Jul 18, 2017 at 9:02 PM, Riccardo Cohen <riccardo.co...@e5group.fr>
wrote:

Can you see any info in the log : login incorrect, database not found etc.
?
(There are many logs in tomcat : localhost log, catalina log, manager log,
host manager log, localhost access log)


On 18/07/2017 13:55, Avinash Krishnan wrote:

Hello Riccardo ,

Thanks for the response. This didn't work for me. Connections are not
getting initated and I am seeing java.lang.NullPointerException on
accessing getConnection.

I  am refering to http://tomcat.apache.org/tomcat-8.0-doc/jdbc-pool.html

The pool properties is also not working.

On Tue, Jul 18, 2017 at 4:18 PM, Riccardo Cohen <
riccardo.co...@e5group.fr>
wrote:

Hello Avinash

I'm not expert but this is rather simple :
in web/META-INF/context.xml write something like :

<Context>
  <Resource name="jdbc/tomcattest"
    type="javax.sql.DataSource"
    username="root"
    password="pass"
    driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/databasename?useSSL=false&
amp;zeroDateTimeBehavior=convertToNull&amp;jdbcCompliantTrun
cation=false&amp;characterEncoding=utf8"
  />
</Context>

in web/WEB-INF/web.xml add in <web-app> tag :
  <resource-ref>
    <res-ref-name>
      jdbc/tomcattest
    </res-ref-name>
    <res-type>
      javax.sql.DataSource
    </res-type>
  </resource-ref>

and in a java class add this :

public class T3Servlet extends HttpServlet
{
  @Resource(name="jdbc/tomcattest")
  public DataSource ds;


You will normally have a data source in your class, by injection, using
tomcat database pool.


On 18/07/2017 12:26, Avinash Krishnan wrote:

I am trying to implement Apache Tomcat 8.5.15  "Tomcat JDBC Connection
Pool" using the steps mentioned in the guide.

Can some one help me to understand how this connection pooling has to be
done.

Is the Plain Java Method,by implementing Pool Properties is an
alternative
to the JNDI lookup based pooling ? When I implement using Pool
Properties,
there isn't any provision to set up the Factory to
org.apache.tomcat.jdbc.pool.DataSourceFactory"
and I always get invalid arguments in call.

On a different note, I tried by adding to context.xml . And implementing
JNDI lookup from context. But that time,I get
"org.apache.tomcat.dbcp.dbcp2.BasicDataSource cannot be cast to
org.apache.tomcat.jdbc.pool.DataSource" even after setting factory to
DataSourceFactory.


--
Riccardo Cohen
+33 6 09 83 64 49
E5Group
http://www.5flow.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org





--
Riccardo Cohen
+33 6 09 83 64 49
E5Group
http://www.5flow.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org





--
Riccardo Cohen
+33 6 09 83 64 49
E5Group
http://www.5flow.com



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to