Announcement Announcement Module
Collapse
No announcement yet.
Transaction problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transaction problem

    Hello!

    I'm new to Spring and trying to get my first examples up and running. Unfortunately, I'm having trouble getting my transaction example to work. I'm using JDO for data access and my Spring configuration file looks like this:

    Code:
    <bean id="pmf" class="com.signsoft.ibo.client.PersistenceManagerFactoryImpl" destroy-method="close">
        <property name="connectionDriverName" value="com.mysql.jdbc.Driver"/>
        <property name="connectionURL" value="jdbc:mysql://localhost/cardb"/>
        <property name="connectionInfoName" value="com.signsoft.ibo.dbsupport.mysql.MySqlDatabaseInfo"/>
        <property name="connectionUserName" value="root"/>
        <property name="optimistic" value="true"/>
        <property name="nontransactionalRead" value="false"/>
      </bean>
    
      <bean id="dao" class="de.znt.springexjdo.dao.JDODao" destroy-method="close">
        <constructor-arg ref="pmf"/>
      </bean>
    
      <bean id="myService" class="de.znt.springexjdo.service.MyServiceImpl">
        <constructor-arg ref="dao"/>
      </bean>
    
      <bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
      	<property name="persistenceManagerFactory" ref="pmf"/>
      </bean>
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
      	<tx:attributes>
    		<tx:method name="get*" read-only="true"/>
    		<tx:method name="*"/>
    	</tx:attributes>
      </tx:advice>
    
      <aop:config>
      	<aop:pointcut id="serviceMethods" expression="execution(* de.znt.springexjdo.service.MyService.*(..))"/>
      	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
      </aop:config>
    When I run the example app, I get a JDO error "Read outside a transaction with nontransactional-read = false". It seems that there is no open transaction. Is there something wrong with my configuration?

    Btw, I am not using the JDO Template, but implemented my DAO with plain JDO.

  • #2
    How are you running your example app? That is the piece/part I mis here.

    Comment


    • #3
      The application is a standard Java command line app:

      Code:
      public class SpringExample
      {
      	public static void main(String[] args)
      	{
      		ApplicationContext ctx = new ClassPathXmlApplicationContext("de/znt/springexjdo/applicationContext_JDOInjection.xml");
      		MyService service = (MyService) ctx.getBean("myService");
      		service.printData();
      	}
      }
      The DAO Interface:
      Code:
      public interface Dao
      {
      	List retrieveData(Class targetClass);
      }
      Implementation of the DAO interface:
      Code:
      public class JDODao implements Dao
      {
      	private PersistenceManagerFactory pmf;
      
      	public JDODao(PersistenceManagerFactory pmf)
      	{
      		this.pmf = pmf;
      	}
      
      	public List retrieveData(Class targetClass)
      	{
      		List resultList = new ArrayList();
      
      		Query query = null;
      		PersistenceManager pm = pmf.getPersistenceManager();
      
      		try
      		{
      			query = pm.newQuery(Car.class);
      			Iterator resIt = ((Collection) query.execute()).iterator();
      
      			while (resIt.hasNext())
      			{
      				resultList.add(resIt.next());
      			}
      		} finally
      		{
      			if (query != null)
      			{
      				query.closeAll();
      			}
      
      			if (pm != null)
      			{
      				pm.close();
      			}
      		}
      
      		return resultList;
      	}
      
      	public void close()
      	{
      	}
      }
      The Service interface:
      Code:
      public interface MyService
      {
      	void printData();
      	void computeSomething();
      }
      And the implementation:
      Code:
      public class MyServiceImpl implements MyService
      {
      	private Dao dao;
      
      	public MyServiceImpl(Dao dao)
      	{
      		this.dao = dao;
      	}
      
      	public void printData()
      	{
      		Iterator carDataIt = ((List) dao.retrieveData(Car.class)).iterator();
      
      		while (carDataIt.hasNext())
      		{
      			Car car = (Car) carDataIt.next();
      			System.out.println(car.getManufacturer() + "/" + car.getType() + "/" + car.getColor());
      		}
      	}
      
      	public void computeSomething()
      	{
      		throw new UnsupportedOperationException();
      	}
      }

      Comment


      • #4
        Since you are not using JdoDaoSupport you should obtain the PersistenceManager using the PersistenceManagerFactoryUtils to get an instance that is bound to the current transaction.

        Try using:
        Code:
        PersistenceManager pm = 
            PersistenceManagerFactoryUtils.getPersistenceManager(pmf, false)
        Also see JavaDoc.

        Comment


        • #5
          Originally posted by trisberg
          Try using:
          Code:
          PersistenceManager pm = 
              PersistenceManagerFactoryUtils.getPersistenceManager(pmf, false)
          Thank's a lot! Its working now.

          Comment

          Working...
          X