2012-07-06 52 views
-1

可能重複:
Configure hibernate to connect to database via JNDI Datasource的Hibernate + MySQL數據庫+ Tomcat的 - 無法連接

我設法通過Hibernate的數據庫訪問,但我得到一個配置的SessionFactory的:空。我在我的tomcat/lib文件夾中有mysql連接器數據庫,而不是在我的項目中。但我認爲它應該像這樣工作。

對於MySQL數據庫:

的context.xml

    <?xml version="1.0" encoding="UTF-8"?> 

    <Context> 

    <!-- myDatabase = database name --> 

    <Resource name="jdbc/myDatabase" 

        auth="Container" 

        type="javax.sql.DataSource" 

        username="admin" 

        password="geheim" 

        driverClassName="com.mysql.jdbc.Driver" 

        url="jdbc:mysql://localhost:3306/myDatabase" 

        maxActive="15" 

        maxIdle="3"/> 

    </Context> 

的web.xml:

<!-- DB --> 
    <resource-ref> 
     <description>MyDatabase Description</description> 
     <res-ref-name>jdbc/myDatabase</res-ref-name> 
     <res-type>javax.sql.DataSource</res-type> 
     <res-auth>Container</res-auth> 
    </resource-ref> 

休眠

的hibernate.cfg.xml:

  <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
     <session-factory> 
      <!-- Database connection settings --> 
       <!-- <property name="connection.datasource">java:comp/env/jdbc/myDatabase</property>--> 
       <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/myDatabase</property> 
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
       <property name="current_session_context_class">thread</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.username">admin</property> 
     <property name="hibernate.connection.password">geheim</property> 

       <!-- <mapping resource="com/bachelor/EstateDTO.hbm.xml"/> --> 
       <mapping resource="com/bachelor/hibernateobject/Event.hbm.xml"/> 
     </session-factory> 
    </hibernate-configuration> 

的Util:

public class HibernateUtil { 
     private static SessionFactory sessionFactory; 
     private static ServiceRegistry serviceRegistry; 

     public static SessionFactory buildSessionFactory() { 
      try { 
       // Create the SessionFactory from hibernate.cfg.xml 
       Configuration configuration = new Configuration(); 
       System.out.println("1"); 
       configuration.configure(); 
       System.out.println("2"); 
       serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
       System.out.println("3"); 
       return new Configuration().configure().buildSessionFactory(serviceRegistry); 
      } catch (Throwable ex) { 
       // Make sure you log the exception, as it might be swallowed 
       System.err.println("Initial SessionFactory creation failed." + ex); 
       throw new ExceptionInInitializerError(ex); 
      } 
     } 

     public static SessionFactory getSessionFactory() { 
      return sessionFactory; 
     } 
    } 

呼叫

MySQL數據庫連接工作與:

  Context initCtx = new InitialContext(); 
      Context envCtx = (Context) initCtx.lookup("java:comp/env"); 
      System.out.println(envCtx); 
      DataSource ds = (DataSource) envCtx.lookup("jdbc/myDatabase"); 
      System.out.println(ds); 

的休眠調用犯規:

Session session = HibernateUtil.buildSessionFactory().openSession(); 
    session.beginTransaction(); 

    Event theEvent = new Event(); 
    theEvent.setTitle("My Event"); 
    theEvent.setDate(new Date()); 
    session.save(theEvent); 

    session.save(theEvent); 

    session.getTransaction().commit(); 

堆棧跟蹤:

06.07.2012 16:52:09 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM' 
06.07.2012 16:52:09  com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder  <init> 
INFO: Instantiated the Application class com.bachelor.core.MyApplication 
06.07.2012 16:52:11 org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
06.07.2012 16:52:11 org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.1.3.Final} 
06.07.2012 16:52:11 org.hibernate.cfg.Environment <clinit> 
NFO: HHH000206: hibernate.properties not found 
06.07.2012 16:52:11 org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
1 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: com/bachelor/hibernateobject/Event.hbm.xml 
06.07.2012 16:52:11 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
2 
3 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: com/bachelor/hibernateobject/Event.hbm.xml 
06.07.2012 16:52:11 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
WARN: HHH000223: Recognized obsolete hibernate namespace h ttp://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 
06.07.2012 16:52:11 org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
06.07.2012 16:52:11 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
06.07.2012 16:52:11  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000115: Hibernate connection pool size: 20 
06.07.2012 16:52:11  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl  configure 
INFO: HHH000006: Autocommit mode: false 
06.07.2012 16:52:11  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl  configure 
INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/myDatabase] 
06.07.2012 16:52:11  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl  configure 
INFO: HHH000046: Connection properties: {user=admin, password=****} 
06.07.2012 16:52:14 org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure 
WARN: HHH000342: Could not obtain connection to query metadata : Communications  link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The  driver has not received any packets from the server. 
06.07.2012 16:52:14 org.hibernate.dialect.Dialect <init> 
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
06.07.2012 16:52:14 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation 
INFO: HHH000422: Disabling contextual LOB creation as connection was null 
06.07.2012 16:52:14  org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
06.07.2012 16:52:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> 
INFO: HHH000397: Using ASTQueryTranslatorFactory 
06.07.2012 16:52:16 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
WARN: SQL Error: 0, SQLState: 08S01 
06.07.2012 16:52:16 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions 
ERROR: Communications link failure 

The last packet sent successfully to the server was 0 milliseconds ago. The  driver has not received any packets from the server. 
06.07.2012 16:52:16 com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException 
SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re- throwing to the HTTP container 
org.hibernate.exception.JDBCConnectionException: Could not open connection 
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:131) 
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) 
at o rg.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) 
at 

好像該應用程序能夠直接建立到數據庫的連接,而不是通過休眠。

編輯:

哪些錯誤,在我的hibernate.cfg.xml我的網址路徑?

編輯:

Hibernate依賴:

  <dependency> 
       <groupId>org.hibernate</groupId> 
       <artifactId>hibernate-core</artifactId> 
       <version>4.1.3.Final</version> 
      </dependency> 

      <!-- Because this is a web app, we also have a dependency on the servlet api. --> 
      <dependency> 
     <groupId>org.apache.tomcat</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>6.0.35</version> 
    </dependency> 

      <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend --> 
      <dependency> 
       <groupId>org.slf4j</groupId> 
       <artifactId>slf4j-simple</artifactId> 
       <version>1.6.4</version> 
      </dependency> 

      <!-- Hibernate gives you a choice of bytecode providers between cglib and javassist --> 
      <dependency> 
       <groupId>javassist</groupId> 
       <artifactId>javassist</artifactId> 
       <version>3.12.1.GA</version> 
      </dependency> 

        <!-- Hibernate library dependecy end --> 

     <dependency> 
      <groupId>javax.transaction</groupId> 
      <artifactId>jta</artifactId> 
      <version>1.1</version> 
     </dependency> 
+0

你把hibernate.cfg.xml文件放在哪裏,你放什麼樣的hibernate庫? – Raman 2012-07-06 16:52:09

+0

cfg位於src/main/java文件夾中。上面的hibernate依賴關係editet。 – 2012-07-06 17:22:07

+0

我沒有得到另一個例外。這似乎沒有關於司機。也許網址是錯的? – 2012-07-06 17:42:49

回答

-1

的代碼是工作。只有MySQL服務器沒有啓動。

+0

你是什麼意思,請你詳細說明一下? – Siddharth 2014-09-30 11:18:44

+0

它與前面的代碼一起工作。我的MySQL服務器的安裝沒有啓動。 – 2014-09-30 14:05:54

+1

沒有開始,對的方式很模糊。我真的希望你能抽出時間正確,完整地回答這個問題。謝謝。 – Siddharth 2014-10-01 04:15:36