How do I add a c3p0 or DBCP connection pool?

Could you please explain to me how to add a c3pO or DBCP standalone connection pool to my connection based JPA project?

I have a persistence.xml file, and every time I want to query the database, I do this:

EntityManagerFactory emf = this.getEntityManagerFactory ();

// Of course, using persistence.xml to set up the factory

EntityManager em = emf.createEntityManager (); ...

Where can I build a bridge between my external connection pool manager and Toplink? Any ideas or links are appreciated.

Regards, Jean

+2


a source to share


2 answers


I don't use Toplink, so I haven't tested this, but my understanding of the various resources found on the net is that you need to provide an implementation SessionCustomizer

. In this implementation, use a class JNDIConnector

to provide an object DataSource

( c3p0 implements the DataSource API) using the setDataSource(javax.sql.DataSource)

.



Adapt the sample from Working with a given JTA data source in Toplink Essentials .

0


a source


I really don't understand what to do. Such a blurry page for a beginner. However, I created the SessionCustomizer class. Here is my customize () method using c3p0:

public void customize(Session session) throws Exception{ 
DataSource ds = DataSources.unpooledDataSource("myServerURL", "login", "pwd"); 
DataSource pooled = DataSources.pooledDataSource(ds); 
JNDIConnector conn = (JNDIConnector)session.getLogin().getConnector(); 
conn.setDataSource(pooled); 
conn.setLookupType(JNDIConnector.STRING_LOOKUP); 
}

      

I don't even think it's right. I put the connection information in clear code, really weird.

Second, in the persistence.xml example from the link, they put:



<non-jta-data-source>java:comp/env/jdbc/DefaultDS</non-jta-data-source>
   <class>sample.MyEntity</class>
   <properties>
     <property name="toplink.session.customizer" value="es.claro.commons.ds.DataSourceSessionCustomizer"/>
   </properties>

      

What should I do specifically for the "non-jta-data-source" tag? Is there a way to put the linkage information in this xml instead of code?

Reference.

0


a source







All Articles