-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: JUnit and JNDI
PostPosted: Tue Jun 28, 2005 9:18 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 8:18 am
Posts: 24
Hi,

If I build my war file and deploy it to JBoss using the following config file :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory name="java:/hibernate/SessionFactory">
       
        <!-- How to use this ?   
        <property name="hibernate.jndi.class">org.jnp.interfaces.NamingContextFactory</property>
        <property name="hibernate.jndi.url">jnp://localhost:1099/</property>
        -->
         
        <!-- JNDI configuration for Sybase
        -->
        <property name="hibernate.connection.datasource">java:SybaseDS</property>       
        <property name="hibernate.dialect">org.hibernate.dialect.SybaseDialect</property>

        <!-- Plugin ConnectionProvider -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       
        <property name="hibernate.transaction.flush_before_completion">true</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
          
   <mapping resource="blahblahblah.hbm.xml"/>

    </session-factory>

</hibernate-configuration>


I don't have any problem at all. The web app running correctly under JBoss.

My problem is, I have a test case, in which I would like to run using JUnit.
The following JUnit task does not work :

Code:
    <!-- Need to put junit.jar into $ANT_HOME/lib !!! -->
    <target name="junit" depends="clean ,prepare ,compile" description="Run the test suite">
        <mkdir dir="${test.out.dir}"/>
        <junit dir="${webinf.dir}/classes" printsummary="yes" jvm="java" fork="true" haltonfailure="yes" showoutput="true">
            <classpath refid="compile.classpath"/>
            <formatter type="plain"/>
            <formatter type="xml"/>
            <test name="example.test.AllTests" todir="${test.out.dir}" haltonfailure="no"/>
        </junit>
    </target>


because if I invoke Ant, I got this error :

[junit] 13:54:14,548 FATAL DatasourceConnectionProvider: Could not find datasource: java:SybaseDS
[junit] 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
[junit] at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
[junit] at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
[junit] at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
[junit] at javax.naming.InitialContext.lookup(InitialContext.java:351)
[junit] at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:44)
[junit] at org.hibernate.connection.ConnectionProviderFactory.newConnectionProvider(ConnectionProviderFactory.java:80)

If I specify the JNDI Class&URL, so it becomes :
Code:
...
        <!-- How to use this ?   
        -->
        <property name="hibernate.jndi.class">org.jnp.interfaces.NamingContextFactory</property>
        <property name="hibernate.jndi.url">jnp://localhost:1099/</property>
...



I got different error :

[junit] 13:59:28,081 FATAL DatasourceConnectionProvider: Could not find datasource: java:SybaseDS
[junit] javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out]
[junit] at org.jnp.interfaces.NamingContext.discoverServer(NamingContext.java:1302)
[junit] at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1382)
[junit] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:579)
[junit] at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
[junit] at javax.naming.InitialContext.lookup(InitialContext.java:351)
[junit] at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:44)

Anybody knows how to configure my hibernate.cfg.xml so I can run my test case ?

Thanks for any help/suggestions/pointers.


Thanks,
nusa.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 4:26 am 
Newbie

Joined: Thu Jun 16, 2005 2:51 am
Posts: 12
Maybe your datasource is not visible outside jboss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 4:58 am 
Newbie

Joined: Thu Jun 16, 2005 2:51 am
Posts: 12
Maybe your datasource is not visible outside jboss


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 6:03 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 8:18 am
Posts: 24
Thanks for your input.

So that's why I had to specify the JNDI Class and URL, include jbossall-client.jar, and run JBoss. But the error still there.
I definetely miss something.

Any takers ?

Thanks,
nusa.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 7:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Datasources in JBoss are not available remotely, as it does not provide any sort of RMI-JDBC capability.

You'll probably need to utilize a direct JDBC access from your unit tests.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 8:39 am 
Beginner
Beginner

Joined: Fri Sep 24, 2004 8:18 am
Posts: 24
Steve,

Thanks for your suggestions.

OK, so if I have the following in my hibernate config for JBoss, it works :

Code:
    <!-- JBoss-JNDI version
    -->
    <session-factory name="java:/hibernate/SessionFactory">
                 
        <!-- JNDI configuration for Sybase
        -->
        <property name "hibernate.connection.datasource">java:SybaseDS</property>

        <property name="hibernate.dialect">org.hibernate.dialect.SybaseDialect</property>

        <!-- Common configuration -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>
        <property name="hibernate.order_updates">true</property>

        <!-- Plugin ConnectionProvider -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
       
        <property name="hibernate.transaction.flush_before_completion">true</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
          
        <mapping resource="abc.xml"/>

   </session-factory>


but if I want to use JUnit, I have to amend it to become ( it works as well ) :

Code:
    <session-factory>
          
        <property name="hibernate.connection.url">jdbc:jtds:sybase://server:port/tempdb;tds=5.0;lastupdatecount=true</property>
        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
        <property name="hibernate.connection.username">x</property>
        <property name="hibernate.connection.password">y</property>
       
        <property name="hibernate.dialect">org.hibernate.dialect.SybaseDialect</property>

        <!- - Common configuration - ->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>
        <property name="hibernate.order_updates">true</property>

        <!- - Plugin ConnectionProvider - ->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
       
        <property name="hibernate.transaction.flush_before_completion">true</property>
        <property name="hibernate.transaction.auto_close_session">true</property>
          
        <mapping resource="abc.xml"/>

    </session-factory>


Instead of playing with the config file to commen / uncomment, depends on the need ( deploy or JUnit ), is there any other solution ?

Thanks,
nusa


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 8:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You can try using a stand-alone transaction manager/datasource for your unti tests. Something like JOTM or Tyrex.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 13, 2005 10:45 am 
Newbie

Joined: Tue May 03, 2005 6:41 pm
Posts: 6
don't you want to try a Cactus and run junit tests within jboss ?


Top
 Profile  
 
 Post subject: Any update? Is there a way?
PostPosted: Fri Apr 28, 2006 10:16 am 
Newbie

Joined: Fri Apr 28, 2006 10:14 am
Posts: 11
Location: Paris, France
I would like to change our Hibernate conf to use a datasource from JNDI but I don't want to disturb the unit tests. I was glad to find this topic but disappointed that a solution was not found.

If anyone has an update please let me know.

-Michael


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.