views:

513

answers:

2

Hi,

I'd like to know how to modify the server.xml file so all my webservices built on axis2 can talk to the DB using Connection Pooling. Each webservice has a different data source, one points to one instance of the DB and the other to another DB server. How do I specify the context that should be used by each service?

Thanks in advance, Pojo

A: 

If you want to use connection pool in your project , Ensure that you have the following code set up for the Tomcat connection pooling to work in context.xml file:

1)Create file with name "context.xml" if it's not exist under directory "WebContent/META-INF/context.xml" with the following content:

For My Project , Please modify it with appropriate value :

<?xml version="1.0" encoding="UTF-8"?> <Context path="/dbcp" docBase="dbcp">  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"   removeAbandoned="true" removeAbandonedTimeout="30" maxActive="80"   maxIdle="30" maxWait="10000" username="sontn" password="nhantien"   driverClassName="org.postgresql.Driver"    url = "jdbc:postgresql://localhost/group8"   useUnicode="true"    characterEncoding="utf-8" characterSetResults="utf8"      /> </Context>

Or you can copy file : context.xml into directory "$Catalian\webapps\axis2\META-INF"

How can you get connection pool?

In your webservice method : create method getConnection() with following content:

public Connection getConnection() {   Connection connection = null;   try {    Context envCtx = (Context) new InitialContext()
     .lookup("java:comp/env");    DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");    connection = ds.getConnection();   } catch (Exception e) {    System.out.println("Connection error: " + e.getMessage());   }   return connection; }

Thanks

Sơn Trần Ngọc
Thanks for your answer
Sơn Trần Ngọc
A: 

The best answer. Thank you very much !

Keas