Where and When does the Driver get instantiated? [message #834434] |
Mon, 02 April 2012 00:12 |
John Steele Messages: 50 Registered: January 2010 |
Member |
|
|
I'm fairly new to using a persistence provider (e.g., EclipseLink, Hibernate) within OSGi, and I'm trying to understand how the EntityManagerFactory gets tied to a specific driver (e.g., EmbeddedDriver), or does it at all?
Lets say I'm using Gemini JPA and Gemini DBAccess, and I have a persistence bundle with a META-INF/persitence.xml file, which specifies the driver as org.apache.derby.jdbc.EmbeddedDriver.
My understanding is that the Gemini JPA bundle is a consumer of the DataSourceFactory services provided by the Gemini DBAccess bundle. The Gemini JPA bundle will then use the javax.persistence.jdbc.driver property within the persistence.xml to decide which DataSourceFactory to use.
Assuming that the Gemini JPA bundle finds the EmbeddedDataSourceFactory, how does it then go about making the actual connection to the database?
It seems like the driver name is only used for locating the correct DataSourceFactory, but is the actual driver ever used? It's a stupid question, because I'm sure it's used to do the actual connecting to the database, but I'm not finding where this happens in the Gemini JPA source. I'm assuming it occurs within GeminiUtil, PUnitInfo, and the specific OSGiJpaProvider.
Where does the driver get instantiated, and how does the specific DataSourceFactory use it?
I appreciate the feedback.
=============
UPDATE
=============
I found where the Driver is instantiated (PlainDriverDataSource), now how does this DataSource get tied to the EntityManagerFactory? The EMF takes the name of the persistence unit and properties as parameters. Does it use one of the properties (e.g., javax.persistence.jdbc.driver) to bind to the database connection?
[Updated on: Mon, 02 April 2012 00:28] Report message to a moderator
|
|
|
|
|
Re: Where and When does the Driver get instantiated? [message #834952 is a reply to message #834878] |
Mon, 02 April 2012 15:48 |
John Steele Messages: 50 Registered: January 2010 |
Member |
|
|
Eduardo Frazão wrote on Mon, 02 April 2012 10:12
This datasource, is a javax.sql.Datasource?
The org.osgi.service.jdbc.DataSourceFactory service has the following methods:
public abstract javax.sql.DataSource createDataSource(...);
public abstract javax.sql.ConnectionPoolDataSource createConnectionPoolDataSource(...);
public abstract javax.sql.XADataSource createXADataSource(...);
Yes, it's javax.sql.DataSource.
Eduardo Frazão wrote on Mon, 02 April 2012 10:12
If Yes, can I create a external Datasource Pool, and inject it somehow, even using the EntitiManagerFactoryBuilder service?
I'd take a look at this: wiki.eclipse.org/Gemini/JPA/Documentation/JPAServices#Using_the_EntityManagerFactoryBuilder_Service
"The EntityManagerFactoryBuilder service is registered regardless of whether the JDBC driver or datasource is accessible, and is used when the user wants to create an EMF and dynamically specify the JDBC properties at runtime. It is less commonly used and the user must rely on their own knowledge that the JDBC datasource they specify, or that is specified in the persistence descriptor, is available."
John Steele
|
|
|
|
|
Re: Where and When does the Driver get instantiated? [message #835011 is a reply to message #834984] |
Mon, 02 April 2012 17:27 |
John Steele Messages: 50 Registered: January 2010 |
Member |
|
|
Hi Eduardo,
I think you can use this property:
properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, getDataSource());
And then create the data source:
private DataSource getDataSource() {
DataSource dataSource = new ...;
dataSource.setServerName("localhost");
dataSource.setPortNumber(1527);
dataSource.setDatabaseName("MyDB");
dataSource.setUser("app");
dataSource.setPassword("app");
return dataSource;
}
I think this is how you would do it. Hope it helps.
[Updated on: Mon, 02 April 2012 17:27] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03803 seconds