Announcement Announcement Module
Collapse
No announcement yet.
[hibernate] loading an JNDI data source Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [hibernate] loading an JNDI data source

    Hello ,

    I'm trying to load my datasource of hibernate from the JNDI of Webspherer, that's my configuration file that's run successfully but without looking up an JNDI configuration. I would like to do so, but with an JNDI config of my datasource. I hope that i have well descripted my request

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "file:/C:/Bin/spring122/dist/spring-beans.dtd">
    
    <beans>
    	<!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
    
    	<!-- DataSource Definition -->
    	<bean id="dataSource"
    		class="com.mchange.v2.c3p0.ComboPooledDataSource"
    		destroy-method="close">
    		<property name="driverClass">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    
    	
    		<property name="jdbcUrl">
    			<value>jdbc:oracle:thin:@myhost:1521:database</value>
    
    		</property>
    		<property name="user">
    			<value>user</value>
    		</property>
    		<property name="password">
    			<value>password</value>
    		</property>
    	</bean>
    
    	<!-- Hibernate SessionFactory Definition -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>				
    				<value>model/bean/client.hbm.xml</value>
    				<value>model/bean/material.hbm.xml</value>
    		
    				
    			</list>
    
    		</property>
    
    		<property name="hibernateProperties">
    			<props>
    		
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.OracleDialect
    				</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.cglib.use_reflection_optimizer">
    					true
    				</prop>
    				<prop key="hibernate.cache.provider_class">
    				 	org.hibernate.cache.HashtableCacheProvider
    				</prop>
    				<prop key="hibernate.c3p0.acquire_increment">1</prop>
    	            <prop key="hibernate.c3p0.minPoolSize">10</prop>
    	            <prop key="hibernate.c3p0.maxPoolSize">100</prop>
    				<prop key="hibernate.c3p0.timeout">100</prop>
    				<prop key="hibernate.c3p0.max_statement">20</prop>  
    				<prop key="hibernate.c3p0.idle_test_period">100</prop>
    	    	<!--  
    	    	<prop key="hibernate.current_session_context_class">org.hibernate.transaction.JDBCTransactionFactory</prop>   
    				**Enable Hibernate's automatic Session context management **
    				<prop key="hibernate.transaction.factory_class">thread</prop>  
    			 -->	
    			
    				</props>
    		</property>
    
    
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Spring Data Access Exception Translator Defintion -->
    	<!-- translation des erreur come elle est au console-->
    	<bean id="jdbcExceptionTranslator"
    		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
    		<property name="dataSource">
    			<ref bean="dataSource" />
    		</property>
    	</bean>
    
    	<!-- Hibernate Template Defintion -->
    	<bean id="hibernateTemplate"
    		class="org.springframework.orm.hibernate3.HibernateTemplate">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    		<property name="jdbcExceptionTranslator">
    			<ref bean="jdbcExceptionTranslator" />
    		</property>
    	</bean>
    
    	<!-- Hibernate Transaction Manager Definition -->
    	<!-- cette classe est ds la le jar jta -->
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref local="sessionFactory" />
    		</property>
    	</bean>
    </beans>
    Thanks

  • #2
    The spring-context xmlns specifically address this.

    Simply add it to your config then do something along the lines of

    Code:
    <jee:jndi-lookup id="dataSource" jndi-name="jdbc/DS" 
    		resource-ref="true" default-ref="devDataSource" />
    The default-ref can be used as a fall back if JNDI look up fails.

    Hope this helps.

    Comment


    • #3
      Thank you travisjwarren for reply.

      I have tried that code but I have that stack trace :
      Code:
      ************* End Display Current Environment *************
      [19/10/09 09:53:50:750 CEST] 00000014 SystemErr     R org.springframework.beans.factory.BeanDefinitionStoreException: Line 12 in XML document from ServletContext resource [/WEB-INF/hibernate-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Le type d'élément "jee:jndi-lookup" doit être déclaré.
      org.xml.sax.SAXParseException: Le type d'élément "jee:jndi-lookup" doit être déclaré.
      	at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
      	at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
      	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
      	at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
      	at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
      	at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
      	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
      	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
      	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
      	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
      	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
      	at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
      	at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
      	at javax.xml.parsers.DocumentBuilder.parse(Unknown Source)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:153)
      	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.importBeanDefinitionResource(DefaultXmlBeanDefinitionParser.java:297)
      	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.parseBeanDefinitions(DefaultXmlBeanDefinitionParser.java:256)
      	at org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.registerBeanDefinitions(DefaultXmlBeanDefinitionParser.java:185)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:238)
      	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:155)
      	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:99)
      	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:115)
      	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:83)
      	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:87)
      	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:262)
      	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:134)
      	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:230)
      	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:156)
      	at org.springframework.web.context.ContextLoaderServlet.init(ContextLoaderServlet.java:81)
      	at javax.servlet.GenericServlet.init(GenericServlet.java:241)
      	at com.ibm.ws.webcontainer.servlet.ServletWrapper.init(ServletWrapper.java:325)
      	at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.init(ServletWrapperImpl.java:165)
      	at com.ibm.ws.webcontainer.servlet.ServletWrapper.initialize(ServletWrapper.java:1588)
      	at com.ibm.wsspi.webcontainer.extension.WebExtensionProcessor.createServletWrapper(WebExtensionProcessor.java:98)
      	at com.ibm.ws.webcontainer.webapp.WebApp.initializeTargetMappings(WebApp.java:605)
      	at com.ibm.ws.webcontainer.webapp.WebApp.commonInitializationFinish(WebApp.java:360)
      	at com.ibm.ws.webcontainer.webapp.WebAppImpl.initialize(WebAppImpl.java:292)
      That's mean that "jee:jndi-lookup must be declared

      that's my new config file :

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "file:/C:/Bin/spring122/dist/spring-beans.dtd">
      
      <beans>
      	<!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
      
      	<!-- DataSource Definition -->
      
      	<jee:jndi-lookup id="dataSource" jndi-name="jdbc/DS" 
      		resource-ref="true" default-ref="jdbc/DS" />
      		
      	<!-- Hibernate SessionFactory Definition -->
      	<bean id="sessionFactory"
      		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="mappingResources">
      			<list>				
      				<value>client.hbm.xml</value>
      				<value>material.hbm.xml</value>
      			
      				
      			</list>
      
      		</property>
      
      		<property name="hibernateProperties">
      			<props>
      		
      				<prop key="hibernate.dialect">
      					org.hibernate.dialect.OracleDialect
      				</prop>
      				<prop key="hibernate.show_sql">true</prop>
      				<prop key="hibernate.cglib.use_reflection_optimizer">
      					true
      				</prop>
      				<prop key="hibernate.cache.provider_class">
      				 	org.hibernate.cache.HashtableCacheProvider
      				</prop>
      				<prop key="hibernate.c3p0.acquire_increment">1</prop>
      	            <prop key="hibernate.c3p0.minPoolSize">10</prop>
      	            <prop key="hibernate.c3p0.maxPoolSize">100</prop>
      				<prop key="hibernate.c3p0.timeout">100</prop>
      				<prop key="hibernate.c3p0.max_statement">20</prop>  
      				<prop key="hibernate.c3p0.idle_test_period">100</prop>
      	    	<!--  
      	    	<prop key="hibernate.current_session_context_class">org.hibernate.transaction.JDBCTransactionFactory</prop>   
      				**Enable Hibernate's automatic Session context management **
      				<prop key="hibernate.transaction.factory_class">thread</prop>  
      			 -->	
      			
      				</props>
      		</property>
      
      
      		<property name="dataSource">
      			<ref bean="dataSource" />
      		</property>
      	</bean>
      
      	<!-- Spring Data Access Exception Translator Defintion -->
      	<!-- translation des erreur come elle est au console-->
      	<bean id="jdbcExceptionTranslator"
      		class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
      		<property name="dataSource">
      			<ref bean="dataSource" />
      		</property>
      	</bean>
      
      	<!-- Hibernate Template Defintion -->
      	<bean id="hibernateTemplate"
      		class="org.springframework.orm.hibernate3.HibernateTemplate">
      		<property name="sessionFactory">
      			<ref bean="sessionFactory" />
      		</property>
      		<property name="jdbcExceptionTranslator">
      			<ref bean="jdbcExceptionTranslator" />
      		</property>
      	</bean>
      
      	<!-- Hibernate Transaction Manager Definition -->
      	<!-- cette classe est ds la le jar jta -->
      	<bean id="transactionManager"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory">
      			<ref local="sessionFactory" />
      		</property>
      	</bean>
      </beans>
      thanks

      Comment


      • #4
        You answer lays in XML Schema-based configuration here

        Change the bean to

        Code:
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        	xmlns:jee="http://www.springframework.org/schema/jee"
        	xsi:schemaLocation="http://www.springframework.org/schema/beans 
        						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        						http://www.springframework.org/schema/jee 
        						http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

        Comment


        • #5
          Thank you,

          I have resolved my problem by putting that code :

          Code:
          <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName" value="jdbc/tssDataSource"/>
          </bean>
          and

          Code:
          <bean id="sessionFactory"
          		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="dataSource">
          			<ref bean="dataSource" />
          		</property>
          </bean>
          Hopes that's help someone else

          Comment

          Working...
          X