You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by David Goodenough <da...@btconnect.com> on 2008/03/31 16:15:49 UTC

How to get I get to the read JDBC Connection object...

I need to get to the real JDBC Connection object so that I can do some
processing using Postgresql extensions (in particular the LargeObject
support).  I do not expect OpenJPA to understand what I am doing, but
I need to access the Connection object and I need what I do to be 
in the same transaction as that used by OpenJPA (so also it needs to
be the same transaction).

The manual suggests:-

OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
OpenJPAConfiguration conf = kemf.getConfiguration();

on page 190, but kemf does not have a getConfiguration method according
to Eclipse (using OpenJPA 1.0.2).

The OpenJPAConfiguration javadoc is not much use either in that it does
tell me where to get it from.

Any ideas?

David

Re: How to get I get to the read JDBC Connection object...

Posted by Ignacio Andreu <pl...@gmail.com>.
Hi,

Thanks for the information, I will see what will be the best way to
integrate your code into the PosgresDictionary to support Streams.

On Tue, Apr 8, 2008 at 4:48 PM, David Goodenough <
david.goodenough@btconnect.com> wrote:

> On Friday 04 April 2008, Ignacio Andreu wrote:
> > I review the conversation If you're using PostgreSQL, the actual
> > InputStream / Reader don't have support for PostgreSQL, although if
> you've
> > a good solution for Large Object in PostgreSQL please e-mail me.
> >
> >
> > - Ignacio
> >
> > On Fri, Apr 4, 2008 at 7:16 PM, Ignacio Andreu <pl...@gmail.com>
> wrote:
> > > Hi,
> > >
> > > The InputStream / reader support is not documented yet, I want to
> provide
> > > some documentation soon.
> > >
> > >
> > > BTW I sent a patch for the InputStream / Reader support a week ago,
> this
> > > patch solves some bugs, Can you review and commit the patch? Thanks in
> > > advance!!
> > >
> > >
> > >
> > >
> > > On Fri, Apr 4, 2008 at 5:32 PM, Patrick Linskey <pl...@gmail.com>
> > >
> > > wrote:
> > > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > > >
> > > > fields
> > > >
> > > > >  > of type InputStream; with this support, OpenJPA will never
> fully
> > > >
> > > > load
> > > >
> > > > >  > the data into memory.
> > > > >
> > > > >  I can see how this will work for readonly values, how about write
> > > >
> > > > ones?
> > > >
> > > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > > >
> > > > InputStream.
> > > >
> > > > I think it hasn't been documented yet. Take a look at
> > > > InputStreamLobTest for an example.
> > > >
> > > > For write values, you simply assign the InputStream field to the
> > > > stream that OpenJPA should read from when streaming to the database:
> > > >
> > > >    @Entity
> > > >    public class Picture {
> > > >        @Persistent private InputStream data;
> > > >
> > > >        public void streamContentsIntoDatabase(InputStream is) {
> > > >            data = is;
> > > >        }
> > > >
> > > >        public InputStream streamContentsFromDatabase() {
> > > >            return is;
> > > >        }
> > > >    }
> > > >
> > > > -Patrick
> > > >
> > > > On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
> > > >
> > > > <da...@btconnect.com> wrote:
> > > > > On Tuesday 01 April 2008, Patrick Linskey wrote:
> > > > >  > Or, even more simply:
> > > > >  >
> > > > >  > em.getTransaction().begin();
> > > > >  > Connection c = OpenJPAPersistence.cast(em).getConnection();
> > > > >
> > > > >  Actually for what I need I need to cast this to a
> > > >
> > > > DelegatingConnection
> > > >
> > > > >  and then get the PGConnection by casting its InnermostDelegate.
> > > > >  But yes, this works - thank you.
> > > > >
> > > > > > try {
> > > > > >
> > > > >  >     ...
> > > > >  > } finally {
> > > > >  >     c.close(); // the connection is a wrapper; this decrements
> the
> > > > >  > reference count
> > > > >  > }
> > > > >  > em.getTransaction().commit();
> > > > >  >
> > > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > > >
> > > > fields
> > > >
> > > > >  > of type InputStream; with this support, OpenJPA will never
> fully
> > > >
> > > > load
> > > >
> > > > >  > the data into memory.
> > > > >
> > > > >  I can see how this will work for readonly values, how about write
> > > >
> > > > ones?
> > > >
> > > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > > >
> > > > InputStream.
> > > >
> > > > >  David
> > > > >
> > > > >  > -Patrick
> > > > >  >
> > > > >  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar
> > > > >  > <pp...@apache.org>
> > > >
> > > > wrote:
> > > > >  > >  Hi,
> > > > >  > >    Following should work:
> > > > >  > >
> > > > >  > >
> > > > >  > >
> > > > >  > >  import javax.persistence.EntityManager;
> > > > >  > >  import org.apache.openjpa.kernel.Broker;
> > > > >  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> > > > >  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> > > > >  > >  import java.sql.Connection;
> > > > >  > >
> > > > >  > >         EntityManager em = emf.createEntityManager();
> > > > >  > >         Broker broker = JPAFacadeHelper.toBroker(em);
> > > > >  > >
> > > > >  > >         JDBCStore store =
> (JDBCStore)broker.getStoreManager();
> > > > >  > >         Connection con = store.getConnection();
> > > > >  > >
> > > > >  > >  David Goodenough-3 wrote:
> > > > >  > >  > I need to get to the real JDBC Connection object so that I
> > > > >  > >  > can
> > > >
> > > > do some
> > > >
> > > > >  > >  > processing using Postgresql extensions (in particular the
> > > >
> > > > LargeObject
> > > >
> > > > >  > >  > support).  I do not expect OpenJPA to understand what I am
> > > >
> > > > doing, but
> > > >
> > > > >  > >  > I need to access the Connection object and I need what I
> do
> > > > >  > >  > to
> > > >
> > > > be
> > > >
> > > > >  > >  > in the same transaction as that used by OpenJPA (so also
> it
> > > >
> > > > needs to
> > > >
> > > > >  > >  > be the same transaction).
> > > > >  > >  >
> > > > >  > >  > The manual suggests:-
> > > > >  > >  >
> > > > >  > >  > OpenJPAEntityManagerFactory kemf =
> > > >
> > > > OpenJPAPersistence.cast(emf);
> > > >
> > > > >  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> > > > >  > >  >
> > > > >  > >  > on page 190, but kemf does not have a getConfiguration
> method
> > > > >  > >  > according to Eclipse (using OpenJPA 1.0.2).
> > > > >  > >  >
> > > > >  > >  > The OpenJPAConfiguration javadoc is not much use either in
> > > >
> > > > that it
> > > >
> > > > >  > >  > does tell me where to get it from.
> > > > >  > >  >
> > > > >  > >  > Any ideas?
> > > > >  > >  >
> > > > >  > >  > David
> > > > >  > >
> > > > >  > >  --
> > > > >  > >  View this message in context:
> > > >
> > > >
> http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-obje
> > > >ct
> > > >
> > > > >  > >...-tp16400383p16401689.html Sent from the OpenJPA Users
> mailing
> > > >
> > > > list
> > > >
> > > > >  > > archive at Nabble.com.
> > > >
> > > > --
> > > > Patrick Linskey
> > > > 202 669 5907
>
>
> Actually it is even easier than I throught;
>
> Postgresql provides BlobInputStream and BlobOutputStream classes which
> do exactly what is needed.
>
> So to write you do:-
>
>                DelegatingConnection conn =
> (DelegatingConnection)OpenJPAPersistence.cast(em).getConnection();
>                try {
>                        conn.setAutoCommit(false);
>                        Connection c = conn.getInnermostDelegate();
>                        if(c.isClosed()) throw new SQLException("innermost
> delegate is closed");
>                        PGConnection pgconn = (PGConnection)c;
>                        LargeObjectManager lom =
> pgconn.getLargeObjectAPI();
>                        long oid = lom.createLO();
>                        LargeObject lo = lom.open(oid,
> LargeObjectManager.WRITE);
>                         OutputStream loos = lo.getOutputStream();
>
> and now you can write to the OutputStream loos in the normal way.
>
> and to read you:-
>
>                        LargeObjectManager lom =
> ((PGConnection)conn).getLargeObjectAPI();
>                        long oid = Long.parseLong(args[0]);
>                        LargeObject lo = lom.open(oid);
>                        InputStream lois = lo.getInputStream();
>
> and you can read in the normal way.
>
> David
>

Re: How to get I get to the read JDBC Connection object...

Posted by David Goodenough <da...@btconnect.com>.
On Friday 04 April 2008, Ignacio Andreu wrote:
> I review the conversation If you're using PostgreSQL, the actual
> InputStream / Reader don't have support for PostgreSQL, although if you've
> a good solution for Large Object in PostgreSQL please e-mail me.
>
>
> - Ignacio
>
> On Fri, Apr 4, 2008 at 7:16 PM, Ignacio Andreu <pl...@gmail.com> wrote:
> > Hi,
> >
> > The InputStream / reader support is not documented yet, I want to provide
> > some documentation soon.
> >
> >
> > BTW I sent a patch for the InputStream / Reader support a week ago, this
> > patch solves some bugs, Can you review and commit the patch? Thanks in
> > advance!!
> >
> >
> >
> >
> > On Fri, Apr 4, 2008 at 5:32 PM, Patrick Linskey <pl...@gmail.com>
> >
> > wrote:
> > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > >
> > > fields
> > >
> > > >  > of type InputStream; with this support, OpenJPA will never fully
> > >
> > > load
> > >
> > > >  > the data into memory.
> > > >
> > > >  I can see how this will work for readonly values, how about write
> > >
> > > ones?
> > >
> > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > >
> > > InputStream.
> > >
> > > I think it hasn't been documented yet. Take a look at
> > > InputStreamLobTest for an example.
> > >
> > > For write values, you simply assign the InputStream field to the
> > > stream that OpenJPA should read from when streaming to the database:
> > >
> > >    @Entity
> > >    public class Picture {
> > >        @Persistent private InputStream data;
> > >
> > >        public void streamContentsIntoDatabase(InputStream is) {
> > >            data = is;
> > >        }
> > >
> > >        public InputStream streamContentsFromDatabase() {
> > >            return is;
> > >        }
> > >    }
> > >
> > > -Patrick
> > >
> > > On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
> > >
> > > <da...@btconnect.com> wrote:
> > > > On Tuesday 01 April 2008, Patrick Linskey wrote:
> > > >  > Or, even more simply:
> > > >  >
> > > >  > em.getTransaction().begin();
> > > >  > Connection c = OpenJPAPersistence.cast(em).getConnection();
> > > >
> > > >  Actually for what I need I need to cast this to a
> > >
> > > DelegatingConnection
> > >
> > > >  and then get the PGConnection by casting its InnermostDelegate.
> > > >  But yes, this works - thank you.
> > > >
> > > > > try {
> > > > >
> > > >  >     ...
> > > >  > } finally {
> > > >  >     c.close(); // the connection is a wrapper; this decrements the
> > > >  > reference count
> > > >  > }
> > > >  > em.getTransaction().commit();
> > > >  >
> > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > >
> > > fields
> > >
> > > >  > of type InputStream; with this support, OpenJPA will never fully
> > >
> > > load
> > >
> > > >  > the data into memory.
> > > >
> > > >  I can see how this will work for readonly values, how about write
> > >
> > > ones?
> > >
> > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > >
> > > InputStream.
> > >
> > > >  David
> > > >
> > > >  > -Patrick
> > > >  >
> > > >  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar
> > > >  > <pp...@apache.org>
> > >
> > > wrote:
> > > >  > >  Hi,
> > > >  > >    Following should work:
> > > >  > >
> > > >  > >
> > > >  > >
> > > >  > >  import javax.persistence.EntityManager;
> > > >  > >  import org.apache.openjpa.kernel.Broker;
> > > >  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> > > >  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> > > >  > >  import java.sql.Connection;
> > > >  > >
> > > >  > >         EntityManager em = emf.createEntityManager();
> > > >  > >         Broker broker = JPAFacadeHelper.toBroker(em);
> > > >  > >
> > > >  > >         JDBCStore store = (JDBCStore)broker.getStoreManager();
> > > >  > >         Connection con = store.getConnection();
> > > >  > >
> > > >  > >  David Goodenough-3 wrote:
> > > >  > >  > I need to get to the real JDBC Connection object so that I
> > > >  > >  > can
> > >
> > > do some
> > >
> > > >  > >  > processing using Postgresql extensions (in particular the
> > >
> > > LargeObject
> > >
> > > >  > >  > support).  I do not expect OpenJPA to understand what I am
> > >
> > > doing, but
> > >
> > > >  > >  > I need to access the Connection object and I need what I do
> > > >  > >  > to
> > >
> > > be
> > >
> > > >  > >  > in the same transaction as that used by OpenJPA (so also it
> > >
> > > needs to
> > >
> > > >  > >  > be the same transaction).
> > > >  > >  >
> > > >  > >  > The manual suggests:-
> > > >  > >  >
> > > >  > >  > OpenJPAEntityManagerFactory kemf =
> > >
> > > OpenJPAPersistence.cast(emf);
> > >
> > > >  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> > > >  > >  >
> > > >  > >  > on page 190, but kemf does not have a getConfiguration method
> > > >  > >  > according to Eclipse (using OpenJPA 1.0.2).
> > > >  > >  >
> > > >  > >  > The OpenJPAConfiguration javadoc is not much use either in
> > >
> > > that it
> > >
> > > >  > >  > does tell me where to get it from.
> > > >  > >  >
> > > >  > >  > Any ideas?
> > > >  > >  >
> > > >  > >  > David
> > > >  > >
> > > >  > >  --
> > > >  > >  View this message in context:
> > >
> > > http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-obje
> > >ct
> > >
> > > >  > >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing
> > >
> > > list
> > >
> > > >  > > archive at Nabble.com.
> > >
> > > --
> > > Patrick Linskey
> > > 202 669 5907


Actually it is even easier than I throught;

Postgresql provides BlobInputStream and BlobOutputStream classes which
do exactly what is needed.

So to write you do:-

		DelegatingConnection conn = 
(DelegatingConnection)OpenJPAPersistence.cast(em).getConnection();
		try {
			conn.setAutoCommit(false);
			Connection c = conn.getInnermostDelegate();
			if(c.isClosed()) throw new SQLException("innermost delegate is closed");
			PGConnection pgconn = (PGConnection)c;
			LargeObjectManager lom = pgconn.getLargeObjectAPI();
			long oid = lom.createLO();
			LargeObject lo = lom.open(oid, LargeObjectManager.WRITE);
			OutputStream loos = lo.getOutputStream();

and now you can write to the OutputStream loos in the normal way.

and to read you:-

			LargeObjectManager lom = ((PGConnection)conn).getLargeObjectAPI();
			long oid = Long.parseLong(args[0]);
			LargeObject lo = lom.open(oid);
			InputStream lois = lo.getInputStream();

and you can read in the normal way.

David

Re: How to get I get to the read JDBC Connection object...

Posted by David Goodenough <da...@btconnect.com>.
On Friday 04 April 2008, Ignacio Andreu wrote:
> I review the conversation If you're using PostgreSQL, the actual
> InputStream / Reader don't have support for PostgreSQL, although if you've
> a good solution for Large Object in PostgreSQL please e-mail me.
>
When I have got everything working I will post the code.  It seems to work
well.  It actually should not be too difficult to be wrapped up as an 
InputStream.  But it does use the LargeObjectManager rather than 
embedded LOB fields.

The write code seems to work, and now I just to dot the i's and cross the
t's on the read code.

The write code reads:-

	private long writeObject(File file, EntityManager em) throws Exception {
		DelegatingConnection conn = 
(DelegatingConnection)OpenJPAPersistence.cast(em).getConnection();
		try {
			conn.setAutoCommit(false);
			Connection c = conn.getInnermostDelegate();
			if(c.isClosed()) throw new SQLException("innermost delegate is closed");
			PGConnection pgconn = (PGConnection)c;
			LargeObjectManager lom = pgconn.getLargeObjectAPI();
			long oid = lom.createLO();
			LargeObject lo = lom.open(oid, LargeObjectManager.WRITE);
			FileInputStream fis = new FileInputStream(file);
			byte[]buffer = new byte[32768];
			int len = 0;
			while( ( len = fis.read(buffer)) > 0) {
				lo.write(buffer, 0, len);
			}
			lo.close();
			fis.close();
			conn.commit();
			return oid;
		} finally {
			try { conn.close(); } catch (Exception e) { /* */ }
		}
	}

This method returns the OID of the large object, reading the object from 
the File.

David
>
> - Ignacio
>
> On Fri, Apr 4, 2008 at 7:16 PM, Ignacio Andreu <pl...@gmail.com> wrote:
> > Hi,
> >
> > The InputStream / reader support is not documented yet, I want to provide
> > some documentation soon.
> >
> >
> > BTW I sent a patch for the InputStream / Reader support a week ago, this
> > patch solves some bugs, Can you review and commit the patch? Thanks in
> > advance!!
> >
> >
> >
> >
> > On Fri, Apr 4, 2008 at 5:32 PM, Patrick Linskey <pl...@gmail.com>
> >
> > wrote:
> > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > >
> > > fields
> > >
> > > >  > of type InputStream; with this support, OpenJPA will never fully
> > >
> > > load
> > >
> > > >  > the data into memory.
> > > >
> > > >  I can see how this will work for readonly values, how about write
> > >
> > > ones?
> > >
> > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > >
> > > InputStream.
> > >
> > > I think it hasn't been documented yet. Take a look at
> > > InputStreamLobTest for an example.
> > >
> > > For write values, you simply assign the InputStream field to the
> > > stream that OpenJPA should read from when streaming to the database:
> > >
> > >    @Entity
> > >    public class Picture {
> > >        @Persistent private InputStream data;
> > >
> > >        public void streamContentsIntoDatabase(InputStream is) {
> > >            data = is;
> > >        }
> > >
> > >        public InputStream streamContentsFromDatabase() {
> > >            return is;
> > >        }
> > >    }
> > >
> > > -Patrick
> > >
> > > On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
> > >
> > > <da...@btconnect.com> wrote:
> > > > On Tuesday 01 April 2008, Patrick Linskey wrote:
> > > >  > Or, even more simply:
> > > >  >
> > > >  > em.getTransaction().begin();
> > > >  > Connection c = OpenJPAPersistence.cast(em).getConnection();
> > > >
> > > >  Actually for what I need I need to cast this to a
> > >
> > > DelegatingConnection
> > >
> > > >  and then get the PGConnection by casting its InnermostDelegate.
> > > >  But yes, this works - thank you.
> > > >
> > > > > try {
> > > > >
> > > >  >     ...
> > > >  > } finally {
> > > >  >     c.close(); // the connection is a wrapper; this decrements the
> > > >  > reference count
> > > >  > }
> > > >  > em.getTransaction().commit();
> > > >  >
> > > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > >
> > > fields
> > >
> > > >  > of type InputStream; with this support, OpenJPA will never fully
> > >
> > > load
> > >
> > > >  > the data into memory.
> > > >
> > > >  I can see how this will work for readonly values, how about write
> > >
> > > ones?
> > >
> > > >  Also looking in the 1.1.0 manual I can not find any reference to
> > >
> > > InputStream.
> > >
> > > >  David
> > > >
> > > >  > -Patrick
> > > >  >
> > > >  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar
> > > >  > <pp...@apache.org>
> > >
> > > wrote:
> > > >  > >  Hi,
> > > >  > >    Following should work:
> > > >  > >
> > > >  > >
> > > >  > >
> > > >  > >  import javax.persistence.EntityManager;
> > > >  > >  import org.apache.openjpa.kernel.Broker;
> > > >  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> > > >  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> > > >  > >  import java.sql.Connection;
> > > >  > >
> > > >  > >         EntityManager em = emf.createEntityManager();
> > > >  > >         Broker broker = JPAFacadeHelper.toBroker(em);
> > > >  > >
> > > >  > >         JDBCStore store = (JDBCStore)broker.getStoreManager();
> > > >  > >         Connection con = store.getConnection();
> > > >  > >
> > > >  > >  David Goodenough-3 wrote:
> > > >  > >  > I need to get to the real JDBC Connection object so that I
> > > >  > >  > can
> > >
> > > do some
> > >
> > > >  > >  > processing using Postgresql extensions (in particular the
> > >
> > > LargeObject
> > >
> > > >  > >  > support).  I do not expect OpenJPA to understand what I am
> > >
> > > doing, but
> > >
> > > >  > >  > I need to access the Connection object and I need what I do
> > > >  > >  > to
> > >
> > > be
> > >
> > > >  > >  > in the same transaction as that used by OpenJPA (so also it
> > >
> > > needs to
> > >
> > > >  > >  > be the same transaction).
> > > >  > >  >
> > > >  > >  > The manual suggests:-
> > > >  > >  >
> > > >  > >  > OpenJPAEntityManagerFactory kemf =
> > >
> > > OpenJPAPersistence.cast(emf);
> > >
> > > >  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> > > >  > >  >
> > > >  > >  > on page 190, but kemf does not have a getConfiguration method
> > > >  > >  > according to Eclipse (using OpenJPA 1.0.2).
> > > >  > >  >
> > > >  > >  > The OpenJPAConfiguration javadoc is not much use either in
> > >
> > > that it
> > >
> > > >  > >  > does tell me where to get it from.
> > > >  > >  >
> > > >  > >  > Any ideas?
> > > >  > >  >
> > > >  > >  > David
> > > >  > >
> > > >  > >  --
> > > >  > >  View this message in context:
> > >
> > > http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-obje
> > >ct
> > >
> > > >  > >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing
> > >
> > > list
> > >
> > > >  > > archive at Nabble.com.
> > >
> > > --
> > > Patrick Linskey
> > > 202 669 5907



Re: How to get I get to the read JDBC Connection object...

Posted by Ignacio Andreu <pl...@gmail.com>.
I review the conversation If you're using PostgreSQL, the actual InputStream
/ Reader don't have support for PostgreSQL, although if you've a good
solution for Large Object in PostgreSQL please e-mail me.


- Ignacio

On Fri, Apr 4, 2008 at 7:16 PM, Ignacio Andreu <pl...@gmail.com> wrote:

> Hi,
>
> The InputStream / reader support is not documented yet, I want to provide
> some documentation soon.
>
>
> BTW I sent a patch for the InputStream / Reader support a week ago, this
> patch solves some bugs, Can you review and commit the patch? Thanks in
> advance!!
>
>
>
>
> On Fri, Apr 4, 2008 at 5:32 PM, Patrick Linskey <pl...@gmail.com>
> wrote:
>
> > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > fields
> > >  > of type InputStream; with this support, OpenJPA will never fully
> > load
> > >  > the data into memory.
> > >  I can see how this will work for readonly values, how about write
> > ones?
> > >  Also looking in the 1.1.0 manual I can not find any reference to
> > InputStream.
> >
> > I think it hasn't been documented yet. Take a look at
> > InputStreamLobTest for an example.
> >
> > For write values, you simply assign the InputStream field to the
> > stream that OpenJPA should read from when streaming to the database:
> >
> >    @Entity
> >    public class Picture {
> >        @Persistent private InputStream data;
> >
> >        public void streamContentsIntoDatabase(InputStream is) {
> >            data = is;
> >        }
> >
> >        public InputStream streamContentsFromDatabase() {
> >            return is;
> >        }
> >    }
> >
> > -Patrick
> >
> > On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
> > <da...@btconnect.com> wrote:
> > > On Tuesday 01 April 2008, Patrick Linskey wrote:
> > >  > Or, even more simply:
> > >  >
> > >  > em.getTransaction().begin();
> > >  > Connection c = OpenJPAPersistence.cast(em).getConnection();
> > >  Actually for what I need I need to cast this to a
> > DelegatingConnection
> > >  and then get the PGConnection by casting its InnermostDelegate.
> > >  But yes, this works - thank you.
> > >
> > > > try {
> > >  >     ...
> > >  > } finally {
> > >  >     c.close(); // the connection is a wrapper; this decrements the
> > >  > reference count
> > >  > }
> > >  > em.getTransaction().commit();
> > >  >
> > >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for
> > fields
> > >  > of type InputStream; with this support, OpenJPA will never fully
> > load
> > >  > the data into memory.
> > >  I can see how this will work for readonly values, how about write
> > ones?
> > >  Also looking in the 1.1.0 manual I can not find any reference to
> > InputStream.
> > >
> > >  David
> > >
> > >
> > > >
> > >  > -Patrick
> > >  >
> > >  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar <pp...@apache.org>
> > wrote:
> > >  > >  Hi,
> > >  > >    Following should work:
> > >  > >
> > >  > >
> > >  > >
> > >  > >  import javax.persistence.EntityManager;
> > >  > >  import org.apache.openjpa.kernel.Broker;
> > >  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> > >  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> > >  > >  import java.sql.Connection;
> > >  > >
> > >  > >         EntityManager em = emf.createEntityManager();
> > >  > >         Broker broker = JPAFacadeHelper.toBroker(em);
> > >  > >
> > >  > >         JDBCStore store = (JDBCStore)broker.getStoreManager();
> > >  > >         Connection con = store.getConnection();
> > >  > >
> > >  > >  David Goodenough-3 wrote:
> > >  > >  > I need to get to the real JDBC Connection object so that I can
> > do some
> > >  > >  > processing using Postgresql extensions (in particular the
> > LargeObject
> > >  > >  > support).  I do not expect OpenJPA to understand what I am
> > doing, but
> > >  > >  > I need to access the Connection object and I need what I do to
> > be
> > >  > >  > in the same transaction as that used by OpenJPA (so also it
> > needs to
> > >  > >  > be the same transaction).
> > >  > >  >
> > >  > >  > The manual suggests:-
> > >  > >  >
> > >  > >  > OpenJPAEntityManagerFactory kemf =
> > OpenJPAPersistence.cast(emf);
> > >  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> > >  > >  >
> > >  > >  > on page 190, but kemf does not have a getConfiguration method
> > >  > >  > according to Eclipse (using OpenJPA 1.0.2).
> > >  > >  >
> > >  > >  > The OpenJPAConfiguration javadoc is not much use either in
> > that it
> > >  > >  > does tell me where to get it from.
> > >  > >  >
> > >  > >  > Any ideas?
> > >  > >  >
> > >  > >  > David
> > >  > >
> > >  > >  --
> > >  > >  View this message in context:
> > >  > >
> > http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object
> > >  > >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing
> > list
> > >  > > archive at Nabble.com.
> > >
> > >
> > >
> >
> >
> >
> > --
> > Patrick Linskey
> > 202 669 5907
> >
>
>

Re: How to get I get to the read JDBC Connection object...

Posted by Ignacio Andreu <pl...@gmail.com>.
Hi,

The InputStream / reader support is not documented yet, I want to provide
some documentation soon.


BTW I sent a patch for the InputStream / Reader support a week ago, this
patch solves some bugs, Can you review and commit the patch? Thanks in
advance!!



On Fri, Apr 4, 2008 at 5:32 PM, Patrick Linskey <pl...@gmail.com> wrote:

> >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
> >  > of type InputStream; with this support, OpenJPA will never fully load
> >  > the data into memory.
> >  I can see how this will work for readonly values, how about write ones?
> >  Also looking in the 1.1.0 manual I can not find any reference to
> InputStream.
>
> I think it hasn't been documented yet. Take a look at
> InputStreamLobTest for an example.
>
> For write values, you simply assign the InputStream field to the
> stream that OpenJPA should read from when streaming to the database:
>
>    @Entity
>    public class Picture {
>        @Persistent private InputStream data;
>
>        public void streamContentsIntoDatabase(InputStream is) {
>            data = is;
>        }
>
>        public InputStream streamContentsFromDatabase() {
>            return is;
>        }
>    }
>
> -Patrick
>
> On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
> <da...@btconnect.com> wrote:
> > On Tuesday 01 April 2008, Patrick Linskey wrote:
> >  > Or, even more simply:
> >  >
> >  > em.getTransaction().begin();
> >  > Connection c = OpenJPAPersistence.cast(em).getConnection();
> >  Actually for what I need I need to cast this to a DelegatingConnection
> >  and then get the PGConnection by casting its InnermostDelegate.
> >  But yes, this works - thank you.
> >
> > > try {
> >  >     ...
> >  > } finally {
> >  >     c.close(); // the connection is a wrapper; this decrements the
> >  > reference count
> >  > }
> >  > em.getTransaction().commit();
> >  >
> >  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
> >  > of type InputStream; with this support, OpenJPA will never fully load
> >  > the data into memory.
> >  I can see how this will work for readonly values, how about write ones?
> >  Also looking in the 1.1.0 manual I can not find any reference to
> InputStream.
> >
> >  David
> >
> >
> > >
> >  > -Patrick
> >  >
> >  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar <pp...@apache.org>
> wrote:
> >  > >  Hi,
> >  > >    Following should work:
> >  > >
> >  > >
> >  > >
> >  > >  import javax.persistence.EntityManager;
> >  > >  import org.apache.openjpa.kernel.Broker;
> >  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> >  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> >  > >  import java.sql.Connection;
> >  > >
> >  > >         EntityManager em = emf.createEntityManager();
> >  > >         Broker broker = JPAFacadeHelper.toBroker(em);
> >  > >
> >  > >         JDBCStore store = (JDBCStore)broker.getStoreManager();
> >  > >         Connection con = store.getConnection();
> >  > >
> >  > >  David Goodenough-3 wrote:
> >  > >  > I need to get to the real JDBC Connection object so that I can
> do some
> >  > >  > processing using Postgresql extensions (in particular the
> LargeObject
> >  > >  > support).  I do not expect OpenJPA to understand what I am
> doing, but
> >  > >  > I need to access the Connection object and I need what I do to
> be
> >  > >  > in the same transaction as that used by OpenJPA (so also it
> needs to
> >  > >  > be the same transaction).
> >  > >  >
> >  > >  > The manual suggests:-
> >  > >  >
> >  > >  > OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
> >  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> >  > >  >
> >  > >  > on page 190, but kemf does not have a getConfiguration method
> >  > >  > according to Eclipse (using OpenJPA 1.0.2).
> >  > >  >
> >  > >  > The OpenJPAConfiguration javadoc is not much use either in that
> it
> >  > >  > does tell me where to get it from.
> >  > >  >
> >  > >  > Any ideas?
> >  > >  >
> >  > >  > David
> >  > >
> >  > >  --
> >  > >  View this message in context:
> >  > >
> http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object
> >  > >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing
> list
> >  > > archive at Nabble.com.
> >
> >
> >
>
>
>
> --
> Patrick Linskey
> 202 669 5907
>

Re: How to get I get to the read JDBC Connection object...

Posted by Patrick Linskey <pl...@gmail.com>.
>  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
>  > of type InputStream; with this support, OpenJPA will never fully load
>  > the data into memory.
>  I can see how this will work for readonly values, how about write ones?
>  Also looking in the 1.1.0 manual I can not find any reference to InputStream.

I think it hasn't been documented yet. Take a look at
InputStreamLobTest for an example.

For write values, you simply assign the InputStream field to the
stream that OpenJPA should read from when streaming to the database:

    @Entity
    public class Picture {
        @Persistent private InputStream data;

        public void streamContentsIntoDatabase(InputStream is) {
            data = is;
        }

        public InputStream streamContentsFromDatabase() {
            return is;
        }
    }

-Patrick

On Fri, Apr 4, 2008 at 6:57 AM, David Goodenough
<da...@btconnect.com> wrote:
> On Tuesday 01 April 2008, Patrick Linskey wrote:
>  > Or, even more simply:
>  >
>  > em.getTransaction().begin();
>  > Connection c = OpenJPAPersistence.cast(em).getConnection();
>  Actually for what I need I need to cast this to a DelegatingConnection
>  and then get the PGConnection by casting its InnermostDelegate.
>  But yes, this works - thank you.
>
> > try {
>  >     ...
>  > } finally {
>  >     c.close(); // the connection is a wrapper; this decrements the
>  > reference count
>  > }
>  > em.getTransaction().commit();
>  >
>  > Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
>  > of type InputStream; with this support, OpenJPA will never fully load
>  > the data into memory.
>  I can see how this will work for readonly values, how about write ones?
>  Also looking in the 1.1.0 manual I can not find any reference to InputStream.
>
>  David
>
>
> >
>  > -Patrick
>  >
>  > On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar <pp...@apache.org> wrote:
>  > >  Hi,
>  > >    Following should work:
>  > >
>  > >
>  > >
>  > >  import javax.persistence.EntityManager;
>  > >  import org.apache.openjpa.kernel.Broker;
>  > >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
>  > >  import org.apache.openjpa.persistence.JPAFacadeHelper;
>  > >  import java.sql.Connection;
>  > >
>  > >         EntityManager em = emf.createEntityManager();
>  > >         Broker broker = JPAFacadeHelper.toBroker(em);
>  > >
>  > >         JDBCStore store = (JDBCStore)broker.getStoreManager();
>  > >         Connection con = store.getConnection();
>  > >
>  > >  David Goodenough-3 wrote:
>  > >  > I need to get to the real JDBC Connection object so that I can do some
>  > >  > processing using Postgresql extensions (in particular the LargeObject
>  > >  > support).  I do not expect OpenJPA to understand what I am doing, but
>  > >  > I need to access the Connection object and I need what I do to be
>  > >  > in the same transaction as that used by OpenJPA (so also it needs to
>  > >  > be the same transaction).
>  > >  >
>  > >  > The manual suggests:-
>  > >  >
>  > >  > OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>  > >  > OpenJPAConfiguration conf = kemf.getConfiguration();
>  > >  >
>  > >  > on page 190, but kemf does not have a getConfiguration method
>  > >  > according to Eclipse (using OpenJPA 1.0.2).
>  > >  >
>  > >  > The OpenJPAConfiguration javadoc is not much use either in that it
>  > >  > does tell me where to get it from.
>  > >  >
>  > >  > Any ideas?
>  > >  >
>  > >  > David
>  > >
>  > >  --
>  > >  View this message in context:
>  > > http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object
>  > >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing list
>  > > archive at Nabble.com.
>
>
>



-- 
Patrick Linskey
202 669 5907

Re: How to get I get to the read JDBC Connection object...

Posted by David Goodenough <da...@btconnect.com>.
On Tuesday 01 April 2008, Patrick Linskey wrote:
> Or, even more simply:
>
> em.getTransaction().begin();
> Connection c = OpenJPAPersistence.cast(em).getConnection();
Actually for what I need I need to cast this to a DelegatingConnection
and then get the PGConnection by casting its InnermostDelegate.
But yes, this works - thank you.
> try {
>     ...
> } finally {
>     c.close(); // the connection is a wrapper; this decrements the
> reference count
> }
> em.getTransaction().commit();
>
> Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
> of type InputStream; with this support, OpenJPA will never fully load
> the data into memory.
I can see how this will work for readonly values, how about write ones?
Also looking in the 1.1.0 manual I can not find any reference to InputStream.

David
>
> -Patrick
>
> On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar <pp...@apache.org> wrote:
> >  Hi,
> >    Following should work:
> >
> >
> >
> >  import javax.persistence.EntityManager;
> >  import org.apache.openjpa.kernel.Broker;
> >  import org.apache.openjpa.jdbc.kernel.JDBCStore;
> >  import org.apache.openjpa.persistence.JPAFacadeHelper;
> >  import java.sql.Connection;
> >
> >         EntityManager em = emf.createEntityManager();
> >         Broker broker = JPAFacadeHelper.toBroker(em);
> >
> >         JDBCStore store = (JDBCStore)broker.getStoreManager();
> >         Connection con = store.getConnection();
> >
> >  David Goodenough-3 wrote:
> >  > I need to get to the real JDBC Connection object so that I can do some
> >  > processing using Postgresql extensions (in particular the LargeObject
> >  > support).  I do not expect OpenJPA to understand what I am doing, but
> >  > I need to access the Connection object and I need what I do to be
> >  > in the same transaction as that used by OpenJPA (so also it needs to
> >  > be the same transaction).
> >  >
> >  > The manual suggests:-
> >  >
> >  > OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
> >  > OpenJPAConfiguration conf = kemf.getConfiguration();
> >  >
> >  > on page 190, but kemf does not have a getConfiguration method
> >  > according to Eclipse (using OpenJPA 1.0.2).
> >  >
> >  > The OpenJPAConfiguration javadoc is not much use either in that it
> >  > does tell me where to get it from.
> >  >
> >  > Any ideas?
> >  >
> >  > David
> >
> >  --
> >  View this message in context:
> > http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object
> >...-tp16400383p16401689.html Sent from the OpenJPA Users mailing list
> > archive at Nabble.com.



Re: How to get I get to the read JDBC Connection object...

Posted by Patrick Linskey <pl...@gmail.com>.
Or, even more simply:

em.getTransaction().begin();
Connection c = OpenJPAPersistence.cast(em).getConnection();
try {
    ...
} finally {
    c.close(); // the connection is a wrapper; this decrements the
reference count
}
em.getTransaction().commit();

Additionally, note that OpenJPA 1.1.0-SNAPSHOT has support for fields
of type InputStream; with this support, OpenJPA will never fully load
the data into memory.

-Patrick

On Mon, Mar 31, 2008 at 8:31 PM, Pinaki Poddar <pp...@apache.org> wrote:
>
>  Hi,
>    Following should work:
>
>
>
>  import javax.persistence.EntityManager;
>  import org.apache.openjpa.kernel.Broker;
>  import org.apache.openjpa.jdbc.kernel.JDBCStore;
>  import org.apache.openjpa.persistence.JPAFacadeHelper;
>  import java.sql.Connection;
>
>         EntityManager em = emf.createEntityManager();
>         Broker broker = JPAFacadeHelper.toBroker(em);
>
>         JDBCStore store = (JDBCStore)broker.getStoreManager();
>         Connection con = store.getConnection();
>
>
>
>
>  David Goodenough-3 wrote:
>  >
>  > I need to get to the real JDBC Connection object so that I can do some
>  > processing using Postgresql extensions (in particular the LargeObject
>  > support).  I do not expect OpenJPA to understand what I am doing, but
>  > I need to access the Connection object and I need what I do to be
>  > in the same transaction as that used by OpenJPA (so also it needs to
>  > be the same transaction).
>  >
>  > The manual suggests:-
>  >
>  > OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>  > OpenJPAConfiguration conf = kemf.getConfiguration();
>  >
>  > on page 190, but kemf does not have a getConfiguration method according
>  > to Eclipse (using OpenJPA 1.0.2).
>  >
>  > The OpenJPAConfiguration javadoc is not much use either in that it does
>  > tell me where to get it from.
>  >
>  > Any ideas?
>  >
>  > David
>  >
>  >
>
>  --
>  View this message in context: http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object...-tp16400383p16401689.html
>  Sent from the OpenJPA Users mailing list archive at Nabble.com.
>
>



-- 
Patrick Linskey
202 669 5907

Re: How to get I get to the read JDBC Connection object...

Posted by Pinaki Poddar <pp...@apache.org>.
import javax.persistence.EntityManager;
import org.apache.openjpa.kernel.Broker;
import org.apache.openjpa.jdbc.kernel.JDBCStore;
import org.apache.openjpa.persistence.JPAFacadeHelper;
import java.sql.Connection;
  
        EntityManager em = emf.createEntityManager();
        Broker broker = JPAFacadeHelper.toBroker(em);

        JDBCStore store = (JDBCStore)broker.getStoreManager();
        Connection con = store.getConnection();




David Goodenough-3 wrote:
> 
> I need to get to the real JDBC Connection object so that I can do some
> processing using Postgresql extensions (in particular the LargeObject
> support).  I do not expect OpenJPA to understand what I am doing, but
> I need to access the Connection object and I need what I do to be 
> in the same transaction as that used by OpenJPA (so also it needs to
> be the same transaction).
> 
> The manual suggests:-
> 
> OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
> OpenJPAConfiguration conf = kemf.getConfiguration();
> 
> on page 190, but kemf does not have a getConfiguration method according
> to Eclipse (using OpenJPA 1.0.2).
> 
> The OpenJPAConfiguration javadoc is not much use either in that it does
> tell me where to get it from.
> 
> Any ideas?
> 
> David
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object...-tp16400383p16401689.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How to get I get to the read JDBC Connection object...

Posted by devu213 <de...@gmail.com>.
Hi Patrick / others,

I know this thread is a bit old but I hope someone replies. Here's the line
of interest to me

>>quote
Don't forget to close() the connection when you're done with it --
OpenJPA does reference counting on the connections it doles out in
order to properly handle the ConnectionRetainMode setting.

>>end quote

I'm currently working with a scenario where I'm using DBUnit for testing.
Dbunit uses raw JDBC connections for its needs. I'm getting separate JDBC
connections for this. However, I chose to leave the connection open as I
want to manage performance for a larger test suite. 

For openJPA I'm using a pattern where I bind the entity manager to a
ThreadLocal object so that repeated calls to getTransaction(),
getEntityManager() return me the same object (a pattern I picked up from my
earlier work in Hibernate). 

Now this seems to throw up weird errors, JPA does not seem to behave as it
should. The moment I close the earlier JDBC connection (used for DBUnit
stuff), it works fine. I've read and digged enuff to conclude that this is a
threading issue and that somewhere the "Connection" bound to the current
thread is causing the problem.

However, I can't explain the entire behaviour to myself (not knowing JPA
internals). Could someone throw light on this. Also, I need a pattern where
I can reuse the JDBC connection. Is this possible at all ?

Right now, I'm gonna probably just resign to my fate and close the
underlying connection....

Please read my post which has the code I've spoken about.

http:// 
http://n2.nabble.com/open-connections-causing-strange-behaviour-td661614.html 








Patrick Linskey-2 wrote:
> 
>>  The manual suggests:-
>>
>>  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>>  OpenJPAConfiguration conf = kemf.getConfiguration();
>>
>>  on page 190, but kemf does not have a getConfiguration method according
>>  to Eclipse (using OpenJPA 1.0.2).
> 
> Oops; that's out-of-date. Getting the Configuration isn't part of the
> published APIs. In addition, that documentation shows how to find a
> separate connection, not the one currently in use by the EM.
> 
> For what you've described, you should use
> OpenJPAEntityManager.getConnection() and cast the returned object to a
> Connection. The returned connection is a wrapper; you may need to dig
> through it to get to the underlying Connection if you need to use
> Postgres-specific methods.
> 
> Don't forget to close() the connection when you're done with it --
> OpenJPA does reference counting on the connections it doles out in
> order to properly handle the ConnectionRetainMode setting.
> 
> Out of curiosity, what types of large object stuff do you need to do?
> 
> -Patrick
> 
> On Mon, Mar 31, 2008 at 7:15 AM, David Goodenough
> <da...@btconnect.com> wrote:
>> I need to get to the real JDBC Connection object so that I can do some
>>  processing using Postgresql extensions (in particular the LargeObject
>>  support).  I do not expect OpenJPA to understand what I am doing, but
>>  I need to access the Connection object and I need what I do to be
>>  in the same transaction as that used by OpenJPA (so also it needs to
>>  be the same transaction).
>>
>>  The manual suggests:-
>>
>>  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>>  OpenJPAConfiguration conf = kemf.getConfiguration();
>>
>>  on page 190, but kemf does not have a getConfiguration method according
>>  to Eclipse (using OpenJPA 1.0.2).
>>
>>  The OpenJPAConfiguration javadoc is not much use either in that it does
>>  tell me where to get it from.
>>
>>  Any ideas?
>>
>>  David
>>
> 
> 
> 
> -- 
> Patrick Linskey
> 202 669 5907
> 
> 

-- 
View this message in context: http://n2.nabble.com/How-to-get-I-get-to-the-read-JDBC-Connection-object...-tp210473p663764.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: How to get I get to the read JDBC Connection object...

Posted by David Goodenough <da...@btconnect.com>.
On Monday 31 March 2008, Patrick Linskey wrote:
> >  The manual suggests:-
> >
> >  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
> >  OpenJPAConfiguration conf = kemf.getConfiguration();
> >
> >  on page 190, but kemf does not have a getConfiguration method according
> >  to Eclipse (using OpenJPA 1.0.2).
>
> Oops; that's out-of-date. Getting the Configuration isn't part of the
> published APIs. In addition, that documentation shows how to find a
> separate connection, not the one currently in use by the EM.
>
> For what you've described, you should use
> OpenJPAEntityManager.getConnection() and cast the returned object to a
> Connection. The returned connection is a wrapper; you may need to dig
> through it to get to the underlying Connection if you need to use
> Postgres-specific methods.
>
> Don't forget to close() the connection when you're done with it --
> OpenJPA does reference counting on the connections it doles out in
> order to properly handle the ConnectionRetainMode setting.
>
> Out of curiosity, what types of large object stuff do you need to do?
I want to use the PGConnection to get a LargeObjectManager, and then
to create a LargeObject, which I can then read and write.  These are 
media objects, which I will then stream to a player, and which I really do
not want to load into memory all at once.  So in my JPA managed object
I will just keep the oid, and then I will use the code above to get the
real data.

I found a way of doing it, but it involves a xxxSPI method which I guess
I should not be using:-

OpenJPAEntityManagerFactorySPI kemf = (OpenJPAEntityManagerFactorySPI) 
OpenJPAPersistence.cast(emf);
OpenJPAConfiguration conf = kemf.getConfiguration();
DataSource ds = (DataSource)conf.getConnectionFactory();
DelegatingConnection delconn = 
(DelegatingConnection)ds.getConnection("david", "");
Connection conn = delconn.getInnermostDelegate();
PGConnection pgconn = (PGConnection)delconn.getInnermostDelegate();

I need the Connection as well as the PGConnection as I can not 
setAutoCommit on a PGConnection.

It would be better to get the actual connection being used for the transaction
in which I persist the object containing the oid, as then the same commit
and rollback would apply to both the persist of the JPA object and the
LargeObject, but if I can not have it I will have to busk it and garbage 
collect the LargeObjects from time to time.

David
>
> -Patrick
>
> On Mon, Mar 31, 2008 at 7:15 AM, David Goodenough
>
> <da...@btconnect.com> wrote:
> > I need to get to the real JDBC Connection object so that I can do some
> >  processing using Postgresql extensions (in particular the LargeObject
> >  support).  I do not expect OpenJPA to understand what I am doing, but
> >  I need to access the Connection object and I need what I do to be
> >  in the same transaction as that used by OpenJPA (so also it needs to
> >  be the same transaction).
> >
> >  The manual suggests:-
> >
> >  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
> >  OpenJPAConfiguration conf = kemf.getConfiguration();
> >
> >  on page 190, but kemf does not have a getConfiguration method according
> >  to Eclipse (using OpenJPA 1.0.2).
> >
> >  The OpenJPAConfiguration javadoc is not much use either in that it does
> >  tell me where to get it from.
> >
> >  Any ideas?
> >
> >  David



Re: How to get I get to the read JDBC Connection object...

Posted by Patrick Linskey <pl...@gmail.com>.
>  The manual suggests:-
>
>  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>  OpenJPAConfiguration conf = kemf.getConfiguration();
>
>  on page 190, but kemf does not have a getConfiguration method according
>  to Eclipse (using OpenJPA 1.0.2).

Oops; that's out-of-date. Getting the Configuration isn't part of the
published APIs. In addition, that documentation shows how to find a
separate connection, not the one currently in use by the EM.

For what you've described, you should use
OpenJPAEntityManager.getConnection() and cast the returned object to a
Connection. The returned connection is a wrapper; you may need to dig
through it to get to the underlying Connection if you need to use
Postgres-specific methods.

Don't forget to close() the connection when you're done with it --
OpenJPA does reference counting on the connections it doles out in
order to properly handle the ConnectionRetainMode setting.

Out of curiosity, what types of large object stuff do you need to do?

-Patrick

On Mon, Mar 31, 2008 at 7:15 AM, David Goodenough
<da...@btconnect.com> wrote:
> I need to get to the real JDBC Connection object so that I can do some
>  processing using Postgresql extensions (in particular the LargeObject
>  support).  I do not expect OpenJPA to understand what I am doing, but
>  I need to access the Connection object and I need what I do to be
>  in the same transaction as that used by OpenJPA (so also it needs to
>  be the same transaction).
>
>  The manual suggests:-
>
>  OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf);
>  OpenJPAConfiguration conf = kemf.getConfiguration();
>
>  on page 190, but kemf does not have a getConfiguration method according
>  to Eclipse (using OpenJPA 1.0.2).
>
>  The OpenJPAConfiguration javadoc is not much use either in that it does
>  tell me where to get it from.
>
>  Any ideas?
>
>  David
>



-- 
Patrick Linskey
202 669 5907