• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clarification on JDBC assignments

 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got two questions on the JDBC assignments.
  • Sharing connections. Is it OK to getConnection() each time a servlet needs a connection, or should we try to get a connection up front and pool or share it somehow?
  • Videos.java. As I see it, you could write this assignment with or without the Videos class. (Well, it would be a modified version.) Is there a preferred way?


  • [ February 25, 2002: Message edited by: Michael Matola ]
     
    Chicken Farmer ()
    Posts: 1932
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Personally, I have each servlet get a connection if needed, and then close it out when done. I would think having a shared connection that is passed around would be viable, though I'm not sure if there are pro's and con's to having a single connection being used like that. I figure if you open the connection only when you need it, you're reducing resources *shrugs*.

    I didn't use a Video.java file, just printed the results as I went, instead of building a separate object and then retrieving the info. Probably could go either way (hope that is what you are referring to).
    Hope that helps
    Jason
     
    Sheriff
    Posts: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Got two questions on the JDBC assignments.
    1. Sharing connections. Is it OK to getConnection() each time a servlet needs a connection ...


    Yes

    2. Videos.java. As I see it, you could write this assignment with or without the Videos class. (Well, it would be a modified version.) Is there a preferred way?

    The JDBC assignments should look very similar to the Servlets-4 assignments. The main difference is that you store the Videos in a database rather than in an ArrayList.

    Does that answer your question?
     
    Michael Matola
    whippersnapper
    Posts: 1843
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    MdQ: Does that answer your question?
    Depends on whether the capital "V" in your preceding sentence was intentional...
    Here's (what I believe to be) a (slightly) less cryptic version of my question: should I modify Video.java so that a given Video object knows how to write itself out to a database and read itself in from a database? Or is there really no need to create actual Video objects at all -- just have the servlets do the selecting and inserting?
    I think what I'm hearing from you and Jason is that there's no need to create individual Video objects. Was just looking for confirmation of that.
     
    Michael Matola
    whippersnapper
    Posts: 1843
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by jason adam:
    I would think having a shared connection that is passed around would be viable, though I'm not sure if there are pro's and con's to having a single connection being used like that. I figure if you open the connection only when you need it, you're reducing resources *shrugs*.


    Don't know how it is with JDBC, but in general I understand that database connections are fairly expensive to establish. So it's not uncommon to reuse them when it's cheaper to have one open all the time (even if it sits idle) than to constantly open and close new ones.
    When I tried to code JDBC-2 with a shared connection though, I was able to reduce the amount of boilerplate database connection code, but ended up increasing the amount of exception-handling code as a consequence. So it looks like a tradeoff there too.


    I didn't use a Video.java file, just printed the results as I went, instead of building a separate object and then retrieving the info. Probably could go either way (hope that is what you are referring to).


    Yep. That's exactly what I was referring to. I had in mind not just the printing part, but also writing to and reading from the database.
    [ February 26, 2002: Message edited by: Michael Matola ]
     
    Ranch Hand
    Posts: 351
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    While learning JDBC last fall I had several assingments that made me create Servlets to manage database connection pools. This builds a lot of flexibility into a system so as it grows it can manage database hits efficiently.
    J2EE containers like WebLogic and WebSphere have the functionality built in. You configure the connection parameters for a database and enter the number of connections to be pooled. That's pretty cool!
     
    Marilyn de Queiroz
    Sheriff
    Posts: 9109
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Originally posted by Michael Matola:
    should I modify Video.java so that a given Video object knows how to write itself out to a database and read itself in from a database?


    No. You should not modify Video.java.

    Or is there really no need to create actual Video objects at all -- just have the servlets do the selecting and inserting?

    right.

    I think what I'm hearing from you and Jason is that there's no need to create individual Video objects. Was just looking for confirmation of that.

    confirmed.
     
    Trailboss
    Posts: 23778
    IntelliJ IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Marilyn asked me to drop a note in here about "pooling".
    True pooling is something well beyond the scope of these assignments. To learn more about it, I suggest you bring it up in the JDBC forum.
    I suggest that you open a new connection each time you need one. If you don't have "true" pooling available, I think this is the best route. The reason is twofold:
    a) Sometimes after a lot of use, a connection "goes stupid". That's why true pooling refreshes connections periodically.
    b) Leaving a connection open while it is not being used is generally considered a bad idea. Many databases permit only a limited number of connections. And as your apps grow, you may have multiple apps accessing the same database at the same time.
    Opening a connection is a slow process and the best solution to all things is pooling - but again, let's not worry about that just yet
    reply
      Bookmark Topic Watch Topic
    • New Topic