Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:46:02+00:00 2026-06-07T14:46:02+00:00

Possible Duplicate: Configure hibernate to connect to database via JNDI Datasource I try to

  • 0

Possible Duplicate:
Configure hibernate to connect to database via JNDI Datasource

I try to get database access via hibernate, but I get a Configured SessionFactory: null. I have the mysql connector db in my tomcat/lib folder, not in my project. But I think it should work like this.

For 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

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;
        }
    }

Call

The mysql db connection works with:

            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);

The hibernate call doesnt:

 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();

Stacktrace:

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

It seems like that the application is able to build a connection to the db directly, but not via hibernate.

edit:

Whats wrong with my url path in my hibernate.cfg.xml?

edit:

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:46:07+00:00Added an answer on June 7, 2026 at 2:46 pm

    The code was working. Only the MySQL server didn’t start.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Hibernate - AnnotationConfiguration deprecated In version 3.2 of Hibernate, I could configure
Possible Duplicate: What’s the difference of `./configure` option `--build`, `--host` and `--target`? This is
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How do you configure an OpenFileDIalog to select folders? I'm using C#
Possible Duplicate: How can I configure Vim to compile C code using Borland’s compiler
Possible Duplicate: how to parse json in javascript My server is configured to send
Possible Duplicate: Creating makefile Hello, I am trying to create the makefiles and configure
Possible Duplicate: How do I configure PHP to have permission to create directories and
Possible Duplicate: Using multiple cores/processors when compiling Java Can javac be configured to benefit
Possible Duplicate: Can .NET intercept and change css files? I have configured IIS6 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.