Problem with postgresql tomcat datasource
Problem with postgresql tomcat datasource
От:
Andrea.Ferrando@marconimobile.com
Дата:
I am using Tomcat 4 on a linux-box with postgresql. I'm trying to set up a
data source.
I followed the instructions on
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html
But the ds.getConnection method waits for an undefined time to give me a
connection.
Any suggestion will be appreciated.
Thanks
Andrea
This is a few lines from my server.xml:
validationQuery
maxWait
5000
maxActive
4
password
password
url
jdbc:postgresql://myserver:5432/mydb
driverClassName
org.postgresql.Driver
maxIdle
2
username
username
and some more from web.xml of my web-app
postgreSQL Datasource
jdbc/mydb
javax.sql.DataSource
Container
I access to the db with this servlet:
public class ProvaConnessioneAlDb extends HttpServlet {
static final private String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("");
out.println("ProvaConnessioneAlDb");
out.println("");
out.println("The servlet has received a POST. This is the
reply.
");
out.println("");
}
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String foo=new String();
try {
Context initContext = new InitialContext();
log("initcontext");
if(initContext == null )
throw new Exception("Boom - No Context");
Context envContext = (Context)initContext.lookup("java:/comp/env");
log("envcontext");
if(envContext == null )
throw new Exception("Boom - No Context");
DataSource ds = (DataSource)envContext.lookup("/jdbc/mydb");
log("datasource");
if (ds != null) {
log("if");
log("Ciao "+ ds.toString());
Connection conn = ds.getConnection();
log("connection");
if(conn != null) {
foo = "Got Connection "+conn.toString();
}
conn.close();
}
} catch (SQLException e) {
e.printStackTrace(out);
out.println(""+e.toString()+"
");
}catch (NamingException e) {
out.println(""+e.toString()+"
");
}
catch (Exception e){
e.printStackTrace(out);
out.println(""+e.toString()+"
");
}
out.println("");
out.println("ProvaConnessioneAlDb");
out.println("");
out.println("The servlet has received a GET. This is the
reply.
");
out.println(""+foo+"
");
out.println("");
}
//Clean up resources
public void destroy() {
}
}
and this is the localhost_myapp.txt log
2002-11-21 10:01:46 StandardContext[/myapp]: Mapped to servlet
'provaconnessionealdb' with servlet path '/provaconnessionealdb' and path
info 'null' and update=true
2002-11-21 10:01:46 provaconnessionealdb: initcontext
2002-11-21 10:01:46 provaconnessionealdb: envcontext
2002-11-21 10:01:46 provaconnessionealdb: datasource
2002-11-21 10:01:46 provaconnessionealdb: if
2002-11-21 10:01:46 provaconnessionealdb: Ciao
org.apache.commons.dbcp.BasicDataSource@dd75a4
Re: Problem with postgresql tomcat datasource
От:
<d.kho@chello.nl>
Дата:
- Your pg-db needs to be accepting database connections, set tcpip to yes in your postgres.conf. - Check that your pg_hba.conf is set correctly. - Check that your any packetfilter or iptables is functioning correctly. - Set to a relatively high level compare to , is the number of open database connections your web applications can handle. - Never call connection.close(). The DataSource implementation should be handling that. - Be sure to have defined the in the right place.