views:

97

answers:

3

Hi there.

I'm new to website development using Java but I've got started with Wicket and make a little website. I'd like to expand on what I've already made (a website with a form, labels and links) and implement database connectivity.

I've looked at a couple of examples, in example Mystic Paste, and I see that they're using Hibernate and Spring. I've never touched Hibernate or Spring before and to be honest the heavy use of annotations scare me a little bit as I haven't really made use of them before, with the exception of supressing warnings and overriding.

At this point I have one Connection object which I set up in the WebApplication class upon initialization. I then retrieve this connection object whenever I need to perform queries. I don't know if this is a bad approach or not for a production web application.

All help is greatly appreciated.

+2  A: 

Bad approach because a Connection object is intended for use by a single thread and web application requests are processed from a pool of thread.

In the best case you'll suffer for big performance problems cause the connection object won't execute queries concurrently.

A solution to this problem is the usage of a connection pool.

Andrea Polci
Thanks for the heads up!
John
+3  A: 

Wicket, Spring and Hibernate is pretty much the standard stack for Wicket applications. Or let's rather say that any web framework, Spring and Hibernate is pretty much the standard stack for any web framework.

Regarding Wicket, injecting objects using @SpringBean inside components is an extremely nice to have feature. Additionally, the OpenSessionInViewFilter manages Hibernate sessions for you (while Hibernate itself takes care of connections).

Therefore, I'd really suggest you look into Spring and Hibernate - both of which don't require annotations, but they are most of the time easier to use than configuration files (typically XML).

If you still don't want to use Spring or Hibernate, I'd suggest you look at the OpenSessionInViewFilter and create something similar yourself: create a connection for each request, use it during one request, close it at the end. As this won't perform very well, you might rather choose to get connections from a pool to which you return it at the end of a request. But instead of writing this code, you could already be injecting beans into your components ;)

sfussenegger
Thanks for the thorough answer. I'll look in to Sprng and Hibernate right away -- why not do it the "right way" from the beginning, huh? :-)
John
though if you're starting from Scratch, do take a look at Guice as an alternative to Spring
Eelco
+1  A: 

If you have time you can dig around Apache Cayenne, it's far more light than Hibernate and for dependency injection combine with Google Guice, again very lightweight. Wicket has wicket-guice subproject, which provides DI in wicket components, much like Spring Context. IMHO it's fair alternative, works very nice so far.

Zeratul