Announcement Announcement Module
Collapse
No announcement yet.
How to get the DataSource from LocalContainerEntityManagerFactoryBean in spring 2.5.? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to get the DataSource from LocalContainerEntityManagerFactoryBean in spring 2.5.?

    Here is the XML config -
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schem...-beans-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="systemProperties" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="searchSystemEnvironment" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="placeholderPrefix" value="+++{" />
    <property name="placeholderSuffix" value="}+++"/>
    </bean>

    <bean id="vmc2DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="true">
    <property name="driverClassName" value="+++{jdbc.driverClassName}+++"/>
    <property name="url" value="+++{jdbc.url}+++"/>
    <property name="username" value="+++{jdbc.username}+++"/>
    <property name="password" value="+++{jdbc.password}+++"/>
    <property name="minIdle" value="5"/>
    <property name="maxIdle" value="15"/>
    <property name="initialSize" value="200"/>
    <property name="maxOpenPreparedStatements" value="100"/>
    <property name="maxWait" value="3000"/>
    <property name="timeBetweenEvictionRunsMillis" value="3600000"/><!-- validate connections once an hour. -->
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persist" />
    <property name="persistenceUnitManager">
    <bean class="com.vmc.util.JPAEntityFinder">
    <property name="dataSource" ref="vmc2DataSource"/>
    </bean>
    </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <bean id="jpaInterceptor" class="org.springframework.orm.jpa.JpaInterceptor" >
    <property name="entityManagerFactory">
    <ref bean="entityManagerFactory" />
    </property>
    </bean>

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor .TransactionInterceptor">
    <constructor-arg index="0" ref="transactionManager" />
    <constructor-arg index="1">
    <bean class="org.springframework.transaction.annotation. AnnotationTransactionAttributeSource"></bean>
    </constructor-arg>
    </bean>

    Here is how I am injecting the entityManager into the java class -

    @PersistenceContext
    private EntityManager entityManager;

    So How can I get the DataSource. From the DataSource I can pull the connection url, ect which is what I am really looking for to help me debug a connection issue I am having. In my application Spring and Hibernate act as if everything is ok in my JDBC processing, but the expected data is not being persisted to the DB. I might add that I am running Spring stand alone from the command line, not from inside a web container.

  • #2
    Please use [ code][/code ] tags when posting code that way it remains readable.

    Also start by removing the JpaInterceptor and the TransactionInterceptor you already have tx:annotation-driven. I also assume you have @Transactional on your services that need to be transactional. Also if you are using 2.5 then use the 2.5 namespace and not the 2.0 namespace.

    Finally make sure you are using an application context and not a bean factory (you are running standalone so you are bootstrapping yourself). Also post some code and also post your persistence.xml.

    Comment


    • #3
      Re-post of XML

      Here is the resource definitions -
      Code:
      <beans xmlns="http://www.springframework.org/schema/beans" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
      
          <bean id="systemProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="searchSystemEnvironment" value="true"/>
                <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
                <property name="placeholderPrefix" value="+++{" />
                <property name="placeholderSuffix" value="}+++"/>
          </bean>
          
      	<bean id="vmc2DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="true">
              <property name="driverClassName" value="+++{jdbc.driverClassName}+++"/>
              <property name="url" value="+++{jdbc.url}+++"/>
              <property name="username" value="+++{jdbc.username}+++"/>
              <property name="password" value="+++{jdbc.password}+++"/>
              <property name="minIdle" value="5"/>
              <property name="maxIdle" value="15"/>
              <property name="initialSize" value="200"/>
              <property name="maxOpenPreparedStatements" value="100"/>
              <property name="maxWait" value="3000"/>
              <property name="timeBetweenEvictionRunsMillis" value="3600000"/><!-- validate connections once an hour. -->
          </bean>
      	
          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="persistenceUnitName" value="persist" />
              <property name="persistenceUnitManager">
      			<bean class="com.vmc.util.JPAEntityFinder">
      				<property name="dataSource" ref="vmc2DataSource"/>
      			</bean>
      		</property>
          </bean>
      
      	
      </beans>
      Here is where the context is getting loaded -

      Code:
      		
      // Get the spring context
      ApplicationContext appContext = new ClassPathXmlApplicationContext("persistApplicationContext.xml");
      Here is the code updating the db -
      Code:
      	
      @PersistenceContext
      private EntityManager entityManager;
      ...
      if (isNewEntity) {
      	entityManager.persist(modelObject);
      } else {
      	entityManager.merge(modelObject);
      }
      if(logger.isDebugEnabled()) {
      	logger.debug("Persist completed. uuid: " + uuid);
      }

      Comment


      • #4
        Here is where the service method is defined as transactional -
        Code:
        	@Override
        	@Transactional
        	public void persist(Map<String, String> persistMap) {

        Comment


        • #5
          transaction logging

          Code:
          2012-04-16 09:37:39,477 DEBUG [com.vmc.biz.persist.MapPersistService] Trying to persist com.vmc.biz.inquiry.ExtendedEduInquiryBO. 
          	uuid: 096359e0-4d1a-11e1-abf9-005056b47c59
          2012-04-16 09:37:39,479 DEBUG [com.vmc.biz.persist.MapPersistService] Persist completed. uuid: 096359e0-4d1a-11e1-abf9-005056b47c59
          2012-04-16 09:37:39,480 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] Initiating transaction commit
          2012-04-16 09:37:39,480 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] Committing JPA transaction on EntityManager 
          	[org.hibernate.ejb.EntityManagerImpl@7e920749]
          2012-04-16 09:37:39,480 DEBUG [org.hibernate.transaction.JDBCTransaction] commit
          2012-04-16 09:37:39,480 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] processing flush-time cascades
          2012-04-16 09:37:39,480 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] dirty checking collections
          2012-04-16 09:37:39,481 DEBUG [org.hibernate.engine.Collections] Collection found: [com.vmc.biz.inquiry.AbstractInquiryBO.leads#2], 
          	was: [com.vmc.biz.inquiry.AbstractInquiryBO.leads#2] (uninitialized)
          2012-04-16 09:37:39,481 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] 
          	Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
          2012-04-16 09:37:39,481 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] 
          	Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections
          2012-04-16 09:37:39,481 DEBUG [org.hibernate.pretty.Printer] listing entities:
          2012-04-16 09:37:39,482 DEBUG [org.hibernate.pretty.Printer] com.vmc.biz.clientmanagement.VerticalBO{id=1, createdBy=vmc, 
          	inquiryClassName=com.vmc.biz.inquiry.EduInquiryBO, description=, updatedDate=2008-10-03 10:20:25, 
          	name=EDUCATION, verticalType=0, createdDate=2008-05-21 14:21:33, updatedBy=vmc}
          2012-04-16 09:37:39,484 DEBUG [org.hibernate.pretty.Printer] com.vmc.biz.inquiry.ExtendedEduInquiryBO{
          	<this map intentionally removed to protect the innocent>}
          2012-04-16 09:37:39,485 DEBUG [org.hibernate.transaction.JDBCTransaction] re-enabling autocommit
          2012-04-16 09:37:39,485 DEBUG [org.hibernate.transaction.JDBCTransaction] committed JDBC Connection
          2012-04-16 09:37:39,485 DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection
          2012-04-16 09:37:39,485 DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ 
          	(open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
          2012-04-16 09:37:39,486 DEBUG [org.springframework.orm.jpa.JpaTransactionManager] Closing JPA EntityManager 
          	[org.hibernate.ejb.EntityManagerImpl@7e920749] after transaction
          2012-04-16 09:37:39,486 DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] Closing JPA EntityManager

          Comment

          Working...
          X