IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New So is this example stupid?
[link|http://www.yolinux.com/TUTORIALS/LinuxTutorialTomcat.html#DATABASE|http://www.yolinux.c...cat.html#DATABASE]

This is representative of what I find googling for how to do db connections with servlets. I'm fairly sure this should be, "How not to do it."

Can anyone point to an example of a servlet connecting to a separate DB object?
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Hell, yeah!
The example isn't even thread safe. A JDBC connection isn't fond of multiple, open Statements and there's no synchronisation. Unless the result set is being kept open for a reason, it and the statement has to be wrapped in a synchronized statement, where the data is dumped into some custom, data object. Only process data outside the critical section to reduce bottlenecks.

Or how about having HTML mixed up with Java. You can't show that to a web designer. If a web designer gives you a web page with the typical, artistic crap in it, it's painful to make a servlet emit it. And then someone will want the look and feel of the website completely changed. That's what JSP's for! A tutorial that teaches JSP but doesn't demonstrate it. Ummm...

If you're not going to learn an MVC framework like Struts, the servlet should extract the raw, data objects appropriate for a page from a core, business model then forward to a JSP page that formats them for display. Separating responsility means more maintainable code.

Following the tutorial means putting the hideousness of JDBC in every page controller. Separate this stuff into a core, business model, for the love of god!

And there's error trapping after writing part of the response, so it's too late to forward to a general error page with a contact 'phone number, apology or whatever. And if workflow has to be changed, such as mistyped data that must be corrected or a rejected transaction, the servlet is forced to emit the appropriate output for all eventualities, which is duplicated across many servlets. Rather than working out to which servlet to forward. Control logic comes before view logic.

I doubt I can give you some links as I now do the Struts-JBoss thing, which is a completely different brand of horror.
Matthew Greet


But we must kill them. We must incinerate them. Pig after pig, cow after cow, village after village, army after army. And they call me an assassin. What do you call it when the assassins accuse the assassin? They lie. They lie and we must be merciful to those who lie.
- Colonol Kurtz, Apocalypse Now.
New Re: Hell, yeah!
I use Spring in Tomcat. For Java stuff it's pretty nice. No EJBs to deal with, but you still get any transaction stuff you need. And the Inversion of Control configuration with Spring is very useful.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Way stupid
Threads - definitely. Whole body of doGet() should be wrapped in a big

synchronized(dbcon) { ... }

Then it would have a prayer of being correct (although you are now single threaded and thus not too scalable). This example really hilights the stupid decision to not construct a new servlet for each request.

Also, it doesn't close the db connection in a destroy() method - so you could end up with a mess of hanging connections over time during development.

What are you trying to do BTW?



"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New Trying to learn The Right Way[tm]
Right now, I've got two ideas for an application to practice on. One is the ever-popular photo album. The other is an admin piece for my online store. Either one is something that I can do by hand more easily than write an app, but I need something to practice on.

Basically, I already understand the advantage of MVC ... I've been trying to do it since before I knew the term for it. I know the db connection shouldn't be in the jsp. I didn't think it should be in a servlet, either, but that's the only examples I could find. All the servlet examples are self-contained, never including other objects.

The O'Reilly servlet page did cover enough about thread-safe servlets to confirm the db shouldn't be in one. I would think by now, with as many people writing java as there are, someone would have put up a step-by-step guide to writing jsp using servlets to connect to a db object. Maybe once I've written it I'll twikify it.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New No such way
really, it depends on what you want to do.

If you were to write an app to do basic db admin - its likely that you'll only have one user at a time and so minimal thread safety and simplicity is going to get the job done and frankly I wouldn't invest any more effort into it than taking the above mentioned servlet and wrapping sychronized block on the connection.

But - if you are going to have more users you need to think more about scalability. Its not free though. Consider a weblog app - one author and a bunch of readers - again you can use the above naive approach with some caching (keeping records in memory as maps) for the read-only uses.

OTOH, maybe you want to write a store - now you could have lots of updaters with people adding items to carts, entering shipping info, etc. So each users probably ought to get his own records cache and you should create a connections pool. Pools are dead easy to write - or you can just go find one somewhere. In fact, I just found one for you.

[link|http://proxool.sourceforge.net/|Proxool] is used by cheap bastards travel. What it does is let you write naive db code and sneaks in a pooling implementation under it. IOW you can write

\nClass.forName("org.logicalcobwebs.proxool.ProxoolDriver");\nconnection = DriverManager.getConnection(url);\n...\nconnection.close();\n


and getConnection actually acquires a connection from the pool. Closing the connection puts it back into the pool. That'll get you scalability on the cheap for simple stuff.

Keep in mind that MVC is not a panacea. You have to decide about model sharing and concurrency issues. For a blog, I'd have one model shared among all users. For a store, each user should get his own model/context. For a really big store, writeable objects should pop up in individual contexts, but read-only objects want to be in a shared context.

Notice that more users results in more work though.




"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New I need to write a small app like it's a big one
First I'll learn how to make it scalable and bulletproof. Then I'll worry about when I can ignore some of that.

As for MVC being a panacea, that's not what I'm thinking. I think that keeping it as a goal makes it more likely I'll ask the questions that may prevent pain farther down the road. I'm more interested in the fifth iteration of a project than the first.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Huh, most people are trying to figure out the opposite
Seriously, just do the simplest thing you can and get it working. Then we can help you elaborate and refactor.

Something like proxool will take you pretty far if its just a crud app.



"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New This is classic
You're driving straight towards the overdesigned application which solves a ton of problems that you weren't ever going to have, and is a complete PITA to work with because of all of the overhead. Plus if it ever did get under load it would fall over because somewhere you'll have a design problem, and because the design is heavyweight, it will take more work to track that down and fix.

Remember that a scaleable application is like a mack truck. Great when you need it, but not very convenient for the daily commute.

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Um, ...
Couldn't you write that first paragraph as a general description of any Java application development endeavor?

[image|/forums/images/warning.png|0|This is sarcasm...]

bcnu,
Mikem

Eine Leute. Eine Welt. Ein F\ufffdhrer.
(Just trying to be accepted in the New America)
New No. Exceptions exist. But generally, yes.
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Not exactly
You're driving straight towards the overdesigned application which solves a ton of problems that you weren't ever going to have ...
Keep in mind what problem I'm actually trying to solve: passing an interview for a java position. I need to be familiar with the techniques for heavyweight development.

In my current job, 90% of what we do each day could be done by a decent programmer who's known the language for about a month. I suspect a lot of internal development is the same way. Draw a form, process the input, update the database, show the results. If we interviewed for that, we'd never know who could handle the other 10%.

So no, I don't need a scalable interface for managing my photos. What I do need is a practice project I can use to learn the techniques I will need for large projects.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Good luck
There are many sets of buzzwords that might be applied to a given large project. You'll need luck to pick the same set for your personal project as your interview wants...

Cheers,
Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
New Ah - so that's what you're trying to do
You didn't say so.

The thing is - most of that kind of architecture is approached incrementally starting with naive and simple and then benchmarking and refactoring as appropriate. A few things are sort of self-evident. Like thread safety is worth pursuing from the beginning. But more elaborate load distribution techniques are typically approached incrementally.

You might do better to do some [link|http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/qid=1107306865/sr=8-2/ref=pd_bbs_2/102-2959288-0102521?v=glance&s=books&n=507846|reading].

For my money, Rod Johnson is pretty much the most objective writer on J2EE out there.



"Whenever you find you are on the side of the majority, it is time to pause and reflect"   --Mark Twain

"The significant problems we face cannot be solved at the same level of thinking we were at when we created them."   --Albert Einstein

"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses."   --George W. Bush
New Time to hit the library
Hope they have it in. They tend to be about a year behind on Java, and that's a 2003.


Interesting thing I found while looking around for used copies of it. In [link|http://www.theserverside.com/articles/article.tss?l=RodJohnsonInterview|this interview] the author says:
Some of the most important parts of the framework used in the book aren't available in existing projects. For example, the JavaBeans packages are more sophisticated than existing equivalents such as the Apache BeanUtils that came from Struts. I would have loved to find a similar JDBC abstraction layer but I couldn't.
I'm currently downloading the db framework you pointed me at, but it looks like for some reason jdbc frameworks seem to be lagging a lot of other pieces.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New That's an old interview.
What you want is the [link|http://www.springframework.org|Spring framework], which is basically everything Rod talks about in his book as an actual project, and then some.

We're using it at work and it's a beaut as far as Java frameworks go. JDBC made sane, EJB-like stuff without EJB headaches, etc.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Gold mine
[link|http://www.theserverside.com/articles/article.tss?l=SpringFramework|This] and even moreso [link|http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html|this] are what I was looking for.

Peter, check them out.
===

Purveyor of Doc Hope's [link|http://DocHope.com|fresh-baked dog biscuits and pet treats].
[link|http://DocHope.com|http://DocHope.com]
New Moochos grassy arse


Peter
[link|http://www.ubuntulinux.org|Ubuntu Linux]
[link|http://www.kuro5hin.org|There is no K5 Cabal]
[link|http://guildenstern.dyndns.org|Home]
Use P2P for legitimate purposes!
New Several copies of Rod's book around here.
It's useful for defusing "I read about this nifty J2EE thinger on the web"-itis.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Q on book.
At your recommendation I bought that book. Then, I changed responsiblities at work (no longer a developer). But I am now responsible for setting up things like Tomcat (and later maybe JBoss) and making sure they're always online. I'm reading up on Tomcat 5 right now (Professional Tomcat 5 by wrox) and I have it running (my still remaining trick is to get an iSeries "Webfacing" app running on it - along with a list of other stuff that has to be fixed). From strictly an admin's POV, would I be better off to go through Rod's book first or, realizing that I will likely never actually write a Java app, would you recommend any other book(s)?

TIA.
bcnu,
Mikem

Eine Leute. Eine Welt. Ein F\ufffdhrer.
(Just trying to be accepted in the New America)
New Rod's book isn't for administrators
Although you're likely to find some material of use, you're better off finding a Tomcat-specific book like you have.

If you anticipate getting into arguments with developers about the Right Way to Do Things, then his book would probably be useful from the standpoint of understanding what's going on and how things should be architected.

Check the contents to see if there aren't a few deployment chapters at the end or what not, though. I don't have my copy handy.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Ok, thanks.
bcnu,
Mikem

Eine Leute. Eine Welt. Ein F\ufffdhrer.
(Just trying to be accepted in the New America)
     When does a java constructor execute? - (drewk) - (56)
         When you say 'new' - (tuberculosis) - (41)
             Word from the IGM on ziwt says ... I was wrong - (drewk) - (40)
                 I think you've got it - (tuberculosis) - (11)
                     Would be nice to say that in the docs - (drewk) - (10)
                         That's missing for most languages - (tuberculosis) - (6)
                             Like Drew, I'm trying to teach myself Java. - (pwhysall) - (5)
                                 "Effective Java" => oxymoron :-) -NT - (ChrisR) - (1)
                                     "Microsoft Works" -NT - (drewk)
                                 What would he know about it? - (tuberculosis) - (2)
                                     That's an oddly visceral response. - (pwhysall) - (1)
                                         Symptomatic of general J-headed attitude - (tuberculosis)
                         Re: Would be nice to say that in the docs - (Arkadiy) - (2)
                             That's it exactly - (drewk)
                             I'd give up on Morphic - (tuberculosis)
                 Now I'm getting frustrated (again) - (drewk) - (27)
                     Re: Now I'm getting frustrated (again) - (admin) - (1)
                         Badness - (jake123)
                     Servlets and business logic - (warmachine) - (22)
                         So is this example stupid? - (drewk) - (21)
                             Hell, yeah! - (warmachine) - (1)
                                 Re: Hell, yeah! - (admin)
                             Way stupid - (tuberculosis) - (18)
                                 Trying to learn The Right Way[tm] - (drewk) - (17)
                                     No such way - (tuberculosis) - (16)
                                         I need to write a small app like it's a big one - (drewk) - (15)
                                             Huh, most people are trying to figure out the opposite - (tuberculosis)
                                             This is classic - (ben_tilly) - (13)
                                                 Um, ... - (mmoffitt) - (1)
                                                     No. Exceptions exist. But generally, yes. -NT - (ben_tilly)
                                                 Not exactly - (drewk) - (10)
                                                     Good luck - (ben_tilly)
                                                     Ah - so that's what you're trying to do - (tuberculosis) - (8)
                                                         Time to hit the library - (drewk) - (3)
                                                             That's an old interview. - (admin) - (2)
                                                                 Gold mine - (drewk) - (1)
                                                                     Moochos grassy arse -NT - (pwhysall)
                                                         Several copies of Rod's book around here. - (admin) - (3)
                                                             Q on book. - (mmoffitt) - (2)
                                                                 Rod's book isn't for administrators - (admin) - (1)
                                                                     Ok, thanks. -NT - (mmoffitt)
                     That's not what they are saying - (tuberculosis) - (1)
                         Servlets and page scoped variables. - (warmachine)
         Re: When does a java constructor execute? - (dshellman) - (13)
             There is a certain amount of value... - (admin) - (1)
                 I already know that - (drewk)
             s/Tomcat/Jetty/ - (tuberculosis) - (10)
                 Based on what? - (admin) - (9)
                     Personal experience - (tuberculosis) - (8)
                         A couple of years back, sure. - (admin) - (7)
                             Oh Tomcat 5 - (tuberculosis) - (6)
                                 Reminds me of Spolsky's piece on Netscape - (FuManChu) - (5)
                                     And I remember disagreeing with it the first time - (ben_tilly) - (4)
                                         Makes sense to me. - (Another Scott) - (1)
                                             Know what you mean - (tuberculosis)
                                         It's a threshold thing - (ChrisR) - (1)
                                             And that point is completely true - (ben_tilly)

Didn't they bury this game in Mexico or something?
203 ms