Question

I would like to use a a while testing my entities a special datasource

https://code.google.com/p/datasource-proxy/

which wraps the original datasource (in my case apache derby's) ClientDataSource

So how can i inject the datasource in my JPA without having a container...?

I tried to use simple-jndi but does not work. (not with eclipse link implementation of JPA2)

Is there a way to bypass the JNDI for the datasource when configuring persistence unit ?

(programatically ?)

Thanks.

Was it helpful?

Solution

I found a way for EclipseLink JPA implementation

import org.eclipse.persistence.config.PersistenceUnitProperties;
//define your datasource before proxyDS - not shown here
//then add this property to entity manager factory prop map

emfProps.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, proxyDS);
EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompanyPU", emfProps);
EntityManager em = emf.createEntityManager();

I still want to find a more generic way for any JPA provider

OTHER TIPS

If you want to set up the dataSource by program without using a container or JNDI lookup, then you can use the following way which contains Apache Derby's ClientDataSource:

public DataSource createDataSource() {
   ClientDataSource dataSource = new ClientDataSource();
   dataSource.setServerName("localhost");
   dataSource.setPortNumber(1527);
   dataSource.setDatabaseName("mytestdb");
   dataSource.setUser("myusername");
   dataSource.setPassword("mypasswd");
   return dataSource;
}

Then you can put this code in your application to get JDBC Connection:

Connection connection = dataSource.getConnection("myusername", "mypasswd");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top