Foros de discusión

get connection from c3p0

thumbnail
Jack Bakker, modificado hace 11 años.

get connection from c3p0

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
any way to get a connection from c3p0 from in a portlet ?
thumbnail
Christianto Sahat, modificado hace 11 años.

RE: get connection from c3p0

Regular Member Mensajes: 179 Fecha de incorporación: 25/09/07 Mensajes recientes
Jack Bakker:
any way to get a connection from c3p0 from in a portlet ?


Do you mean to get connection from Liferay's data source ?

How about :
InfrastructureUtil.getDataSource() ?
thumbnail
Jack Bakker, modificado hace 11 años.

RE: get connection from c3p0

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
how the heck did I miss that after all these years...

humble thanks
thumbnail
Jack Bakker, modificado hace 11 años.

RE: get connection from c3p0

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
InfrastructureUtil.getDataSource() does well to grab a connection from lportal db

however, my need here currently is to get a connection from a 2nd datasource (to another non-lportal database) where I have that 2nd datasource defined in META-INF/ext-spring in a 'services' project

ideas ?
thumbnail
Jack Bakker, modificado hace 11 años.

RE: get connection from c3p0

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
what I just did (and it seems to work) was:

I added to one of my *entityNameHere*LocalServiceImpl a new method:

public DataSource getDataSource(){
return *entityNameHere*Persistence.getDataSource();
}

then after regen of services, I call from in the portlet:

DataSource ds = *entityNameHere*LocalServiceUtil.getDataSource()

and then get a connection from the datasource ; I am using this to get connections for a JasperReports solution

is this safe, acceptable ?
thumbnail
David H Nebinger, modificado hace 11 años.

RE: get connection from c3p0

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
May work within the same portlet (so you're not crossing class loader boundaries), but I doubt it would work for other plugins.

You may want to take a look at the wiki. I think there was a recent wiki page update for using liferay SB code in a jasper report as a datasource...
thumbnail
Jack Bakker, modificado hace 11 años.

RE: get connection from c3p0

Liferay Master Mensajes: 978 Fecha de incorporación: 3/01/10 Mensajes recientes
perhaps it is this one you are referring to

http://www.liferay.com/community/wiki/-/wiki/Main/How+to+Use+LiferayServiceBuilderClasses+in+JasperReports+and+IReport

wiki offers a useful approach for a JRBeanCollectionDataSource

in my case I have reports that also use implementations of the JRDataSource and others that require a sql.DataSource and some reports that use both at same time.

So far I am using the below to get a sql.DataSource which works

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/jasperConn");

but I want to use a connection pool c3p0 and config the pool through portal-ext.properties like we do for the lportal db
thumbnail
David H Nebinger, modificado hace 11 años.

RE: get connection from c3p0

Liferay Legend Mensajes: 14914 Fecha de incorporación: 2/09/06 Mensajes recientes
I believe you can define your c3p0-wrapped datasource when defining in JNDI. Would give you c3p0 w/o worrying about portal-ext.properties setup...

The problem w/ the portal-ext.properties approach is that the portal really isn't going to create and/or manage your connection if you define it there. Properties would be available, but unless the Liferay core knew to create that datasource (like it does for lportal), it wouldn't get created nor used.