Guice with Jdbc

2,046 views
Skip to first unread message

Karthik Krishnan

unread,
Feb 27, 2010, 10:01:31 PM2/27/10
to google-guice
Hi All,

In our application, we are slowly migrating to Guice from no DI but we
have hit a little bit of a road block. In our application, we use
CallableStatement to make database calls and not any persistence API.
Is there any guice based application that we can use for transactional
support. I know that warp api is used to support Hibernate, JPA and
and DB4Objects.

A post on Stack Overflow http://stackoverflow.com/questions/2347384/guice-jdbc-and-managing-database-connections
did not return any answers as yet. Dhanji's blogs http://www.jroller.com/dhanji/
and http://rethrick.wordpress.com/ did not help with that either.

Has any one done any plain jdbc set up with Guice?

Thanks,

Kartik

Dhanji R. Prasanna

unread,
Feb 27, 2010, 10:46:29 PM2/27/10
to google...@googlegroups.com
Someone wrote an Ibatis module which maybe able to help you. Alternatively you can use warp-persist with JPA and just obtain the underlying connection to execute your *Statements. That should provide all the transactional behavior you need via the @Transactional annotation. Though this may seem a bit hacky to you, I don't know.

Dhanji.


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.


Kartik Kumar

unread,
Feb 27, 2010, 11:18:34 PM2/27/10
to google...@googlegroups.com
Let me take a look at IbaGuice. I hope you won't mind if I post some stupid questions on the forum. I will try my best to search the warp forums and guice forums before posting a question but then you never know.

Alen Vrečko

unread,
Feb 28, 2010, 4:02:56 AM2/28/10
to google-guice
FWIW I have done plain jdbc with Guice, a year ago for a project. It
was very easy to set up. I ended up with:

o) A way to configure and bind DataSource
x) I didn't bother with providing support for connection pool, as
Oracle Driver comes with build-in CP sames goes Postgresql
o) Did bother on making sure DS is properly closed and driver
unregistered on webapp redeploy or shutdown
o) Made a provider for a Connection (I "hardcoded" connection-per-
transaction approach)
o) Made a transaction interceptor

It looked like

@JdbcTransaction // connection is obtained from the DS set to thread
local
public void doStuff(){
Connection con = connectionProvider.get(); // connection
retrieved from TL
....
} // connection is properly closed(returned to CP) in any situation

If you tried to obtain a Connection outside @JdbcTransaction you'd get
an error.

I also had @Retry(times=3,delay=2) on the methods but that is another
story.

HTH

Cheers
Alen

On Feb 28, 4:01 am, Karthik Krishnan <krishnan.1...@gmail.com> wrote:
> Hi All,
>
> In our application, we are slowly migrating to Guice from no DI but we
> have hit a little bit of a road block. In our application, we use
> CallableStatement to make database calls and not any persistence API.
> Is there any guice based application that we can use for transactional
> support.  I know that warp api is used to support Hibernate, JPA and
> and DB4Objects.
>

> A post on Stack Overflowhttp://stackoverflow.com/questions/2347384/guice-jdbc-and-managing-da...


> did not return any answers as yet. Dhanji's blogshttp://www.jroller.com/dhanji/

> andhttp://rethrick.wordpress.com/did not help with that either.

Eelco Hillenius

unread,
Feb 28, 2010, 4:33:02 PM2/28/10
to google...@googlegroups.com
> x) I didn't bother with providing support for connection pool, as
> Oracle Driver comes with build-in CP sames goes Postgresql

If you use e.g. c3po's datasource, you'll get pooling built in but
independent of the actual driver.

> o) Made a provider for a Connection (I "hardcoded" connection-per-
> transaction approach)

That means that you'll always have a transaction when you want to use
a connection, even if you don't really need one right? As an
alternative you could implement a workmanager-ish solution (similar to
what warp does).

Eelco

Dhanji R. Prasanna

unread,
Feb 28, 2010, 5:43:56 PM2/28/10
to google...@googlegroups.com
Just curious, when would you not need a transaction?

Dhanji.

Eelco Hillenius

unread,
Feb 28, 2010, 6:11:31 PM2/28/10
to google...@googlegroups.com
> Just curious, when would you not need a transaction?

I guess I should say that you don't always want to turn off autocommit
(transaction per execution instead of per annotated method). And
thinking about it it, maybe there is no good reason.

Eelco

Kartik Kumar

unread,
Mar 1, 2010, 4:46:18 PM3/1/10
to google...@googlegroups.com
@Alen What is @Retry annotation? Was this one of the application specific annotations that you implemented?

Alen Vrečko

unread,
Mar 2, 2010, 8:16:14 AM3/2/10
to google-guice
The @Retry just calls the method again if exception is thrown. Just
need to make sure the RetryInterceptor is the first one installed to
avoid any surprises.

In my case the database server got restarted from time to time. That
is nothing special but didn't want the Connection Pool to validate
connections all the time, therefore ConnectionClosedException happens.
But the @Retry just calls the method again, the CP is smart enough to
give a good connection the second time, and the user doesn't get en
error.

Cheers
Alen

On 1 mar., 16:46, Kartik Kumar <krishnan.1...@gmail.com> wrote:
> @Alen What is @Retry annotation? Was this one of the application specific
> annotations that you implemented?
>
> On Sun, Feb 28, 2010 at 3:11 PM, Eelco Hillenius

> <eelco.hillen...@gmail.com>wrote:


>
>
>
> > > Just curious, when would you not need a transaction?
>
> > I guess I should say that you don't always want to turn off autocommit
> > (transaction per execution instead of per annotated method). And
> > thinking about it it, maybe there is no good reason.
>
> > Eelco
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-guice" group.
> > To post to this group, send email to google...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > google-guice...@googlegroups.com<google-guice%2Bunsubscribe@google groups.com>

bklough

unread,
Mar 3, 2010, 1:10:35 PM3/3/10
to google-guice
How do you support transactions now? Traditionally, there's a wrapper
class with Template Methods providing the begin, execute, end sequence
and commit/rollback handling. CP30's connection pooling has worked
great and deals with reconnects, stale connections, etc. without much
customizing at all.

In and of itself, Guice won't provide much unless there's a JDBC
abstraction layer (Templatized DAO's for instance) for it to work
with. We have such here, so Guice affords us the ability to just swap
out ConnectionPool's, Transaction Strategies, etc.. simply by using
different Guice Modules.


On Feb 27, 7:01 pm, Karthik Krishnan <krishnan.1...@gmail.com> wrote:
> Hi All,
>
> In our application, we are slowly migrating to Guice from no DI but we
> have hit a little bit of a road block. In our application, we use
> CallableStatement to make database calls and not any persistence API.
> Is there any guice based application that we can use for transactional
> support.  I know that warp api is used to support Hibernate, JPA and
> and DB4Objects.
>

> A post on Stack Overflowhttp://stackoverflow.com/questions/2347384/guice-jdbc-and-managing-da...


> did not return any answers as yet. Dhanji's blogshttp://www.jroller.com/dhanji/

> andhttp://rethrick.wordpress.com/did not help with that either.

Kartik Kumar

unread,
Mar 6, 2010, 11:04:42 PM3/6/10
to google...@googlegroups.com
I apologize in advance for the long winded email. By Alen's suggestion, I first tried to get a thread local connection instance to be injected. So I first copied an implementation of ThreadLocalScope from Guice Custom Scope implementation http://pastie.org/857790.
Then I injected this scope into my ThreadLocalConnectionProvider to inject the scope to provide initial seed value and to provide fall back connection instance http://pastie.org/857829. To get a thread local connection instance, I call the scope method of my scope implementation. My modules binding is given here. http://pastie.org/857810.
My unit tests work as in I get the same connection instance every time.
My question is related to exception handling. I inject data source to any Provider that requires a connection. DataSource#getConnection() throws SQLException in its method signature. So in my get() implementation, I have to swallow it or throw unchecked exception in its place as it I have done in my example. I know that I can used ThrowingProviders, but scope method does not allow an implementation of that interface as one of its method argument. Is there a better way to do this write this implementation or am I stuck with what I have?

Kartik


To unsubscribe from this group, send email to google-guice...@googlegroups.com.

Alen Vrečko

unread,
Mar 7, 2010, 8:56:50 AM3/7/10
to google-guice
Looks a bit complicated. My original implementation is locked away.
But from memory this is what I had in mind http://pastie.org/858152
and test http://pastie.org/858153. No seal of approval thou.

Maybe bklough can share their implementation. Sounds like they have a
polished jdbc support.

HTH
Alen

On Mar 7, 5:04 am, Kartik Kumar <krishnan.1...@gmail.com> wrote:
> I apologize in advance for the long winded email. By Alen's suggestion, I
> first tried to get a thread local connection instance to be injected. So I
> first copied an implementation of ThreadLocalScope from Guice Custom Scope

> implementationhttp://pastie.org/857790.


> Then I injected this scope into my ThreadLocalConnectionProvider to inject
> the scope to provide initial seed value and to provide fall back connection

> instancehttp://pastie.org/857829. To get a thread local connection


> instance, I call the scope method of my scope implementation. My modules

> binding is given here.http://pastie.org/857810.


> My unit tests work as in I get the same connection instance every time.
> My question is related to exception handling. I inject data source to any
> Provider that requires a connection. DataSource#getConnection() throws
> SQLException in its method signature. So in my get() implementation, I have
> to swallow it or throw unchecked exception in its place as it I have done in
> my example. I know that I can used ThrowingProviders, but scope method does
> not allow an implementation of that interface as one of its method argument.
> Is there a better way to do this write this implementation or am I stuck
> with what I have?
>
> Kartik
>

> > > > google-guice...@googlegroups.com<google-guice%2Bunsu...@googlegroups.com>


> > <google-guice%2Bunsubscribe@google groups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/google-guice?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-guice" group.
> > To post to this group, send email to google...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > google-guice...@googlegroups.com<google-guice%2Bunsu...@googlegroups.com>

nino martinez wael

unread,
Mar 13, 2010, 4:17:20 AM3/13/10
to google...@googlegroups.com
Hmm could'nt we just use the spring jdbc template?

Maybe just using, http://code.google.com/p/guiceyfruit/ ?

regards Nino

2010/3/7 Alen Vrečko <alen_...@yahoo.com>
To unsubscribe from this group, send email to google-guice...@googlegroups.com.

nino martinez wael

unread,
Mar 13, 2010, 4:21:20 AM3/13/10
to google...@googlegroups.com
oeh and the link for it:)

http://static.springsource.org/spring/docs/2.0.x/reference/jdbc.html (could'nt find the 3.0 article)

2010/3/13 nino martinez wael <nino.mart...@gmail.com>
Reply all
Reply to author
Forward
0 new messages