Announcement Announcement Module
Collapse
No announcement yet.
schemaexport with standalone app: NoInitialContextException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • schemaexport with standalone app: NoInitialContextException

    im trying to load my application context from a standalone java app, and run a schemaexport. i am using tomcat6 with a jndi datasource. the configuration/code and stacktrace is below. notice i'm using the "&" symbol on one of the lines:
    appContext.getBean("&mySessionFactory");

    i did this because someone on the forums said it was necessary, does anyone know why?


    Code:
     <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/jdbc/datasource"  />
        </bean>
    Code:
     public class TableCreator {
    	 
    	 public TableCreator() {
    	 }
    	
    	 // define the spring application context files
    	 private static final String[] SPRING_CONFIG_FILES = 
    		 new String[]{"/war/WEB-INF/applicationContext.xml", "/war/WEB-INF/dbSetupContext.xml"};
    	 
    	   
    	   public static void main (String[] args){
    	     new TableCreater().create();
    	   }
    
    	  public void create(){
    		FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext(SPRING_CONFIG_FILES);
    		LocalSessionFactoryBean l = (LocalSessionFactoryBean) appContext.getBean("&mySessionFactory");
    		Configuration cfg = l.getConfiguration();
    		SchemaExport schemaExport = new SchemaExport(cfg);
    		schemaExport.create(false,true);
    	}
    }
    Code:
    log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDataSource' defined in file [C:\myWorkspace\myapp\war\WEB-INF\dbSetupContext.xml]: Invocation of init method failed; nested exception is 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 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:729)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
    	at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:94)
    	at myapp.dbcontrol.TableCreater.create(TableCreater.java:35)
    	at myapp.dbcontrol.TableCreater.main(TableCreater.java:26)
    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(Unknown Source)
    	at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    	at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    	at javax.naming.InitialContext.lookup(Unknown Source)
    	at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
    	at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:88)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:153)
    	at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178)
    	at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95)
    	at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105)
    	at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:200)
    	at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:186)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
    	... 16 more
    Last edited by ew0kian; Jan 10th, 2009, 08:58 PM.

  • #2
    did some googling and found that the api docs for localsessionfactorybean states that you should use getBean("&mySessionFactory") to get the localsessionfactorybean object instead of the hibernate sessionFactory when calling the createSchema method

    i saw this and decided to try using the createDatabaseSchema method that comes packacged with spring instead of using the hibernate class and instead of pointing to a jndi datasource in my context i just defined the c3p0 configuration completely in there:

    Code:
    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost/db?autoReconnect=true" />
    <property name="user" value="root" />
    <property name="password" value="**********" />
    <property name="minPoolSize" value="10"/>
    <property name="maxPoolSize" value="200"/>
    <property name="maxIdleTime" value="21600"/>
    <property name="acquireIncrement" value="5"/>
    
    </bean>
    and the schemaExport was successful!

    here's my revised code, notice i commented out the use of Hibernate's Configuration and SchemaExport classes:

    Code:
    	  public void create(){
    		FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext(SPRING_CONFIG_FILES);
    		LocalSessionFactoryBean l = (LocalSessionFactoryBean) appContext.getBean("&mySessionFactory");
    		 l.createDatabaseSchema();
    		//Configuration cfg = l.getConfiguration();
    		//SchemaExport schemaExport = new SchemaExport(cfg);
    		//schemaExport.create(false,true);
    	  }
    I am glad that this standalone schemaexport app works properly now, but I'm worried that when I try to deploy my app in tomcat and use the jndi configuration it will not fail
    Last edited by ew0kian; Jan 11th, 2009, 12:57 AM.

    Comment


    • #3
      anyone know what i should do to get the jndi configuration working? what are the benefits / drawbacks of configuring the datasource in tomcat as a jndi datasource or in the application context?

      Comment


      • #4
        Originally posted by ew0kian View Post
        anyone know what i should do to get the jndi configuration working? what are the benefits / drawbacks of configuring the datasource in tomcat as a jndi datasource or in the application context?
        You should use InitialContext(Hashtable) instead of InitialContext() at the standalone environment. Provided environment should contain connectivity details to be able to access remote jndi registry. I recommend you to read jndi tutorial to get more information about that.

        Comment


        • #5
          i'm sorry i don't quite understand what you are suggesting

          Comment

          Working...
          X