4 Replies Latest reply: Feb 5, 2013 12:46 PM by 988716 RSS

    problem with connect to sql server ..

    988716

      I have problem with connect to sql server2005
      i use jpa(hibernate) and jsf

      javax.servlet.ServletException: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
      javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)


      root cause

      javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not 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:794)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      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:593)


      root cause

      javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
      org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
      org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
      org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1371)
      org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:60)
      servlet.PrzychodniaBean.dodaj(PrzychodniaBean.java:30)
      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:262)
      org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
      com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      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:794)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      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:593)


      root cause

      org.hibernate.exception.JDBCConnectionException: Could not open connection
      org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131)
      org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
      org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)
      org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
      org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
      org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
      org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1263)
      org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)
      servlet.PrzychodniaBean.dodaj(PrzychodniaBean.java:30)
      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:262)
      org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
      com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      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:794)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      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:593)


      root cause

      java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=MIS
      java.sql.DriverManager.getConnection(Unknown Source)
      java.sql.DriverManager.getConnection(Unknown Source)
      org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:173)
      org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276)
      org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
      org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
      org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
      org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
      org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1263)
      org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:57)
      servlet.PrzychodniaBean.dodaj(PrzychodniaBean.java:30)
      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:262)
      org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
      com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      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:794)
      javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
      com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      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:593)


      note The full stack trace of the root cause is available in the JBoss Web/7.0.13.Final logs.





      persistance.xml
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      <persistence-unit name="PrzychodnieLekarskiePU" transaction-type="RESOURCE_LOCAL">
      <class>model.Przychodznia</class>
      <properties>
      <property name="hibernate.connection.username" value="a"/>
      <property name="hibernate.connection.password" value="a"/>
      <property name="hibernate.connection.url" value="jdbc:sqlserver://localhost;databaseName=MIS"/>
      <property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost;databaseName=MIS"/>
      <property name="javax.persistence.jdbc.user" value="a"/>
      <property name="javax.persistence.jdbc.password" value="a"/>
      <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>

      </properties>
      </persistence-unit>
      </persistence>


      Edited by: 985713 on 2013-02-02 07:12

      Edited by: 985713 on 2013-02-02 07:37

        • 1. Re: problem with connect to sql server ..
          Joe Weinstein-Oracle
          The last 'root cause' is your basic problem. It seems you don't have the sqlserver JDBC driver jar in your classpath.
          • 2. Re: problem with connect to sql server ..
            988716
            on WEB-INF\lib I have sqljdbc4.jar after deploy so I think classpath is ok ..

            I use jboss 7.1.1 FINAL

            Edited by: 985713 on 2013-02-02 11:35
            • 3. Re: problem with connect to sql server ..
              939520
              I suggest you create a new class that has a main() method. In it, create and configure a datasource (url, userID, password, etc). Run it to see if you can get a connection. This way, you can see what's wrong with your url, userID, password, etc; independent of hibernate, JBoss, etc.
              • 4. Re: problem with connect to sql server ..
                988716
                it works ok : I don't known where is error in jpa .. ?



                public class MyConnection {
                     public static Connection getConnection () throws SQLException{
                          
                          String url = "jdbc:sqlserver://ABADDON1;databaseName=MIS";
                          String user = "a";
                          String pass = "a";
                          DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
                          Connection conn = DriverManager.getConnection(
                                    url,user,pass);
                          System.out.println("OK");;
                          return conn;
                          
                     }
                }

                try {
                               conn = MyConnection.getConnection();
                               
                               
                               
                               } catch (SQLException ex) {
                                    System.out.println("Error: " + ex.getErrorCode()
                                              + ex.getMessage());
                               }