views:

340

answers:

3

I write an Test Java APP and it works right BUt this Web app throws an exception like that with to same cfg.xml file

<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.release_mode">auto</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.password">1234</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.default_schema">public</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>

<property name="hibernate.ejb.discard_pc_on_close">false</property>
<property name="hibernate.query.jpaql_strict_compliance">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.transaction.flush_before_completion">false</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.generate_statistics">false</property>
<property name="hibernate.use_sql_comments">false</property>
<property name="hibernate.connection.pool_size">100</property>

When I press "Save" button I get the following exception:

javax.servlet.ServletException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)

root cause

javax.faces.el.EvaluationException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
 com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 javax.faces.component.UICommand.broadcast(UICommand.java:315)
 javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

root cause

org.hibernate.exception.JDBCConnectionException: Cannot open connection
 org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:98)
 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
 org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
 org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
 org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 java.lang.reflect.Method.invoke(Unknown Source)
 org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
 $Proxy108.beginTransaction(Unknown Source)
 com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 java.lang.reflect.Method.invoke(Unknown Source)
 org.apache.el.parser.AstValue.invoke(AstValue.java:196)
 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
 com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 javax.faces.component.UICommand.broadcast(UICommand.java:315)
 javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

root cause

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres
 java.sql.DriverManager.getConnection(Unknown Source)
 java.sql.DriverManager.getConnection(Unknown Source)
 org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
 org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
 org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
 org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
 org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
 org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1463)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 java.lang.reflect.Method.invoke(Unknown Source)
 org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
 $Proxy108.beginTransaction(Unknown Source)
 com.yemex.beans.CompanyBean.saveOrUpdate(CompanyBean.java:52)
 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 java.lang.reflect.Method.invoke(Unknown Source)
 org.apache.el.parser.AstValue.invoke(AstValue.java:196)
 org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
 com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
 javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
 com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
 javax.faces.component.UICommand.broadcast(UICommand.java:315)
 javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
 javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
 com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
 com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
 com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
 javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)

Blockquote

+1  A: 

What's the question here?

The exception is pretty explicit:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

Make sure you can connect to the database you are connected to, that the configuration is correct, that you do not have network issues preventing the connection, etc.

matt b
But I configure hibernate cfg.xml llike that; <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.password">1234</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.default_schema">public</property> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
Omer Salmanoglu
Ok, and do you have a postgres database running on localhost at the default port, and does it have a database named postgres, and does it have a user named postgres with a password named postgres?
matt b
Yes There is a database which is name postgres and is used default portThis db have a user which is name postgres and its pass = '1234'
Omer Salmanoglu
A: 

I didn't look at your config file, but I'm assuming it configured at least reasonably well since the stack trace indicates that it is trying to connect to a postgres database at a localhost JDBC url.

It looks like the big problem is that it can't find the postgres JDBC driver:

root cause

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres java.sql.DriverManager.getConnection(Unknown Source) java.sql.DriverManager.getConnection(Unknown Source) org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)

Make sure the JAR file is in your classpath. You can download it here, if you don't already have it: http://jdbc.postgresql.org/

Jeff
I write a test Java app and it works right but I think that this Web application have a problem. And I control JDBC driver and it is right
Omer Salmanoglu
+2  A: 

When encountering an exception, it's worth to check the bottommost root cause in the stacktrace. In this case it's the following:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost/postgres

This just means that either the URL is wrong or that the desired driver is not present in the current runtime classpath. Since the URL looks fine as per the PostgreSQL JDBC documentation, it'll be that the driver is missing in the current runtime classpath.

So, to fix this problem, you need put the JDBC driver JAR file in the webapp's runtime classpath. The /WEB-INF/lib is one of the folders which is covered by the webapp's default runtime classpath. Just drop the PostgreSQL JDBC driver JAR file in there and redeploy.

BalusC
The Driver path is already /WEB-INF/lib but same problem continues
Omer Salmanoglu
Apparently a different classloader is been used. Try putting in `Tomcat/lib` or adding JAR file's root path to `shared.loader` in `Tomcat/conf/catalina.properties`.
BalusC