Foren

Home » Liferay Portal » English » 3. Development

Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Jack Bakker
get connection from c3p0
6. September 2012 19:04
Antwort

Jack Bakker

Rang: Liferay Master

Nachrichten: 882

Eintrittsdatum: 3. Januar 2010

Neue Beiträge

any way to get a connection from c3p0 from in a portlet ?
Christianto Sahat
RE: get connection from c3p0
6. September 2012 20:10
Antwort

Christianto Sahat

Rang: Regular Member

Nachrichten: 176

Eintrittsdatum: 24. September 2007

Neue Beiträge

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() ?
Jack Bakker
RE: get connection from c3p0
7. September 2012 06:30
Antwort

Jack Bakker

Rang: Liferay Master

Nachrichten: 882

Eintrittsdatum: 3. Januar 2010

Neue Beiträge

how the heck did I miss that after all these years...

humble thanks
Jack Bakker
RE: get connection from c3p0
7. September 2012 06:41
Antwort

Jack Bakker

Rang: Liferay Master

Nachrichten: 882

Eintrittsdatum: 3. Januar 2010

Neue Beiträge

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 ?
Jack Bakker
RE: get connection from c3p0
7. September 2012 08:18
Antwort

Jack Bakker

Rang: Liferay Master

Nachrichten: 882

Eintrittsdatum: 3. Januar 2010

Neue Beiträge

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 ?
David H Nebinger
RE: get connection from c3p0
7. September 2012 10:59
Antwort

David H Nebinger

Community Moderator

Rang: Liferay Legend

Nachrichten: 11771

Eintrittsdatum: 1. September 2006

Neue Beiträge

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...
Jack Bakker
RE: get connection from c3p0
8. September 2012 09:09
Antwort

Jack Bakker

Rang: Liferay Master

Nachrichten: 882

Eintrittsdatum: 3. Januar 2010

Neue Beiträge

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
David H Nebinger
RE: get connection from c3p0
8. September 2012 10:03
Antwort

David H Nebinger

Community Moderator

Rang: Liferay Legend

Nachrichten: 11771

Eintrittsdatum: 1. September 2006

Neue Beiträge

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.