0

I am trying to create a data source in Tomcat 7 for using in Hibernate code.

Tomcat 7.0.47

Hibernate: 4.2.7.Final

OS: Windows 7

context.xml

Used the documetation here to configure the data source in the context.xml file.

context.xml location: <%Tomcat HOME%>\conf

<Resource name="hmsDS" auth="Container" type="javax.sql.DataSource"
                   maxActive="100" maxIdle="30" maxWait="10000"
                   username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost/hms"/>

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.datasource">java:comp/env/hmsDS</property>
        <property name="hibernate.hbm2ddl.auto">validate</property>
        <property name="show_sql">true</property>

        <mapping class="org.srs.hms.entity.TestEntity"/>

    </session-factory>
</hibernate-configuration>

On starting TomCat Console output:

Jan 10, 2014 5:13:24 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\InstalledSoftware\Java\jdk1.7.0_25\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files (x86)\PC Connectivity Solution\;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x86;C:\Program Files (x86)\Intel\OpenCL SDK\2.0\bin\x64;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\MySQL\MySQL Utilities 1.3.4\;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files (x86)\VisualSVN Server\bin;C:\Program Files\TortoiseGit\bin;C:\Program Files (x86)\Microsoft CHESS\bin\;D:\InstalledSoftware\Python25;D:\InstalledSoftware\GNUstep\bin;D:\InstalledSoftware\GNUstep\GNUstep\System\Tools;D:\InstalledSoftware\apache-maven-3.1.1\bin;"C:\Program Files (x86)\WinMerge";.
Jan 10, 2014 5:13:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jan 10, 2014 5:13:24 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jan 10, 2014 5:13:24 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 436 ms
Jan 10, 2014 5:13:24 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jan 10, 2014 5:13:24 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Jan 10, 2014 5:13:25 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jan 10, 2014 5:13:25 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jan 10, 2014 5:13:25 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 353 ms

Code that accesses the database:

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

    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.configure();
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);

        Session sess = sessionFactory.openSession();
        sess.beginTransaction();

        sess.save(new TestEntity());
        sess.getTransaction().commit();
    }
}

On accessing the database from the code above I get the following error:

Caused by: org.hibernate.service.jndi.JndiException: Error parsing JNDI name [java:comp/env/hmsDS]
    at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:92)
    at org.hibernate.service.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:63)
    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:116)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1822)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1780)
    at org.srs.hms.dao.CommonDAO.<clinit>(CommonDAO.java:28)
    ... 27 more
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:499)
    at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
    ... 40 more

Number of connection to the database is '2'

Query: show global status where variable_name = 'Threads_connected';

I ran the query after running my test class. The query result = 2

Question 1 I am not seeing any connection pool related debug/trace statement in the logs when I restart Tomcat after configuring the data source. I was expecting info related to creation of the connections in the connection pool etc. The logging level is set at 'TRACE'. Is this normal?

Question 2 How do I resolve the error shown above?

Update 1 - web.xml

web.xml is updated with the following entry inside tag DB Connection hmsDB javax.sql.DataSource Container

This change didn't help either.

Update 2 I could finally get it working with the suggestions in the comments section. Thanks everyone.

Here is the result of my analysis:

Entry in web.xml is NOT required. Entry made in context.xml is good enough to get it working. Initially I was testing with a stand alone Java class (main method), hence the error mentioned above. It won't work as the stand alone class with not have access to the required context outside the container; an obvious rookie mistake by me. When I tested from the UI, it worked fine.

Special thanks to Joe Rinehart for asking the right question! +1

8
  • 1
    You've left out your web.xml configuration. Did you follow the how-to page, doing that as well (a resource-ref?) Commented Jan 10, 2014 at 12:43
  • I deliberately left the web.xml configuration as I thought it is not required as I am working at the hibernate level only. To instruct the container to create the database connection pool, is context.xml configuration not sufficient? Commented Jan 10, 2014 at 12:47
  • Updating web.xml doesn't seem to help; same error reported. I've updated my original post with this info. Commented Jan 10, 2014 at 12:55
  • 1
    How/in what container are you executing the Test class? Commented Jan 10, 2014 at 13:01
  • Not sure if this could be affecting it, but you are missing the port in your jndi url configuration: url="jdbc:mysql://<server>:<port>/<database>" Commented Jan 10, 2014 at 13:15

1 Answer 1

1

It sounds like you may not be executing your test class within the same context/container/jvm as Tomcat itself. I'd check that out.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.