database access, sort of a style question

Discussion in 'Java' started by JGH, Jan 25, 2005.

  1. JGH

    JGH Guest

    I'm trying to make some objects for accessing database tables. I have a
    super class that connects and does inserts, updates, & deletes:

    class dbtable {
    protected Connection conn;

    protected connect ()
    { ...}

    private insert ()
    { ... }public update ()
    { ...}
    public delete ()
    { ... }
    }

    public table1 extends dbtable { ... }
    public table2 extends dbtable { ... }


    Waht if I want table1 and table 2 to use the same connection? If I
    instantiate table1 and table2, they won't automatically share the
    connection. I could overload the connect method so that if you pass in a
    Connection, it just sets it's onw conn to the object passed in. Then you
    could optionally call it with an already established connection. Like
    this:

    class dbtable {
    protected connect (Connection cn) {
    this.conn = cn;
    }
    }
    ..

    table1 t1 = new table1 ();
    t1.connect();
    tabl2 t2 = new table2 ();
    t2.connect (t1.conn);

    Good idea or no? Any better ideas?
     
    JGH, Jan 25, 2005
    #1
    1. Advertisements

  2. JGH wrote
    A permanently assignment of a connection to a table class
    is a bad idea.

    DataBase Connections are limited ressources.
    Better is using a database connection only for one db access.

    An good idea is modelling bean classes as representation
    of database records.


    --
    Heiner Kuecker
    Internet: http://www.heinerkuecker.de http://www.heiner-kuecker.de
    JSP WorkFlow PageFlow Page Flow FlowControl Navigation: http://www.control-and-command.de
    Java Expression Formula Parser: http://www.heinerkuecker.de/Expression.html
    CnC Template Technology http://www.heinerkuecker.de/Expression.html#templ
    Domain Specific Languages http://www.heinerkuecker.de/DomainParser.html
     
    Heiner Kücker, Jan 25, 2005
    #2
    1. Advertisements

  3. JGH

    JGH Guest

    Huh?
     
    JGH, Jan 25, 2005
    #3
  4. JGH

    foo Guest

    Waht if I want table1 and table 2 to use the same connection? If I
    Use a static variable -- all instances will then share the
    same connection. Another approach is to add a connection
    parameter to all your methods and then you can obtain
    a connection (maybe from a connection pool) and pass it
    to you update/insert etc methods...

    Regardless, you typically want to do this:

    con.setAutoCommit(false);
    ...save to table 1
    ...save to table 2
    .... if all succeeds
    con.commit();
    ...else
    con.rollback();

    --foo
     
    foo, Jan 26, 2005
    #4
  5. JGH

    Alan Krueger Guest

    Instead of this, I'd recommend using the Data Access Object Pattern. It
    decouples the data and database interactions.

    http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
     
    Alan Krueger, Jan 26, 2005
    #5
  6. JGH

    Alan Krueger Guest

    JDBC database connections are (IIRC) not thread-safe, so sharing one
    among all theads is probably not a good idea.
     
    Alan Krueger, Jan 26, 2005
    #6
  7. JGH

    anonymous Guest

    We use a connection pool which is a Singleton implementation. Has a
    getInstance().getConnection() method and a closeConnection() method. The
    Connection object gets passed to methods/classes
     
    anonymous, Jan 26, 2005
    #7
  8. JGH

    Bryce Guest

    Have your controlling class manage the connection . (note: below is
    just quick quasi-psuedo code. it won't compile, but should give you an
    idea of what to do)

    Connection conn = //get connection
    try {
    Table1 t1 = new Table1();
    t1.connect(conn);
    Table2 t2 = new table2();
    t2.connect(conn);
    except() {}
    finally {
    conn.close();
    }
     
    Bryce, Jan 26, 2005
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...