views:

8

answers:

1

Is it possible to Autowire the JDO PersistenceManager?

In the example below, only the PersistenceManagerFactory is Autowired, while the PersistenceManager is obtained using a getter and utility method before each operation.

import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;

@Service
public class MainServiceImpl implements MainService
{

    @Autowired
    private PersistenceManagerFactory pmf;

    private PersistenceManager pm;

    public void setPersistenceManager(PersistenceManager pm)
    {
        this.pm = pm;
    }

    public void setPmf(PersistenceManagerFactory pmf)
    {
        this.pmf = pmf;
    }

    public PersistenceManagerFactory getPmf()
    {
        return pmf;
    }

    public PersistenceManager getPersistenceManager()
    {
        return PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
    }

}
A: 

It doesn't look likely.

If you have a look at the Spring JDO classes overview you'll see the is no FactoryBean that returns a PersistenceManager.

Of course you can easily implement a factory bean yourself that has a dependency of type PersistenceManagerFactory and returns a PersistenceManager.

You may want to internally use the TransactionAwarePersistenceManagerFactoryProxy if you always want to autowire the current thread-bound PersistenceManager (here's an excerpt from the JavaDoc)

Proxy for a target JDO PersistenceManagerFactory, returning the current thread-bound PersistenceManager (the Spring-managed transactional PersistenceManager or the single OpenPersistenceManagerInView PersistenceManager) on getPersistenceManager(), if any.

seanizer