No problem - we were all new once.

I probably shouldn't have suggested using the same connection as the
EntityManager. Getting the connection from an EntityManager instance is kind
of hack and it will tie you to a specific JPA vendor (OpenJPA in your case).
Generally a better solution when you're running in a JEE container is to
obtain a connection from a managed datasource.

Say you have a persistence.xml like this :

<persistence-unit name="foo">
     . . .
     <jta-data-source>jdbc/myDataSource</jta-data-source>
      . . .
</persistence-unit>

Then in your application where you have the persistence context you would do
something like this :

InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("jdbc/myDataSource");  // same name
as persistence.xml
Connection con = ds.getConnection();
con.prepareCall(. . . );
con.close();

The reason this works is because most JEE containers will attempt to share a
single connection within a JTA transaction. So the EntityManager you
injected, and the connection from the DataSource will _usually_ be the same.
There are some conditions where the container can't share a connection (e.g.
if the isolation level has been changed), and each application server may
handle these cases slightly differently.

If you still want to use the same connection as the EntityManager there are
instructions on this thread: http://markmail.org/message/pwoare2l76rqonws

Best Regards,

-mike


On Thu, Mar 31, 2011 at 2:59 PM, chintan4181 <[email protected]> wrote:

> Thnak Michale.
>
> When you say Manually, you mean to get the connection from EntityManager
> and
> use callableStatement?
> If yes, can you show me how to get connection from EntityManagerFactory?
>
> I am using @persistentContext and get the EntityManager out of it. I don't
> see any method for getting JDBC conenction.
>
> I am new to JPA so sorry for the simple question.
>
> Thanks
> Chintan
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Read-Multiple-Resultset-from-Stored-Proc-call-using-Open-JPA-tp6223507p6228576.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Reply via email to