可能重复:
Configure hibernate to connect to database via JNDI Datasource
我尝试通过hibernate获取数据库,但是我得到了一个Configured SessionFactory:null。我的tomcat / lib文件夹中有mysql连接器db,而不是我的项目。但我认为它应该是这样的。
对于MySQL DB:
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);
hibernate调用不会:
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
似乎应用程序能够直接与db建立连接,但不能通过hibernate建立连接。
编辑:
我的hibernate.cfg.xml中的url路径出了什么问题?
编辑:
Hibernate Dependencies:
<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 :(得分:-1)
代码正在运行。只有MySQL服务器没有启动。