database access, sort of a style question

J

JGH

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?
 
H

Heiner Kücker

JGH wrote
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?

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
 
J

JGH

Heiner Kücker said:
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.
Huh?
 
F

foo

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

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
 
A

Alan Krueger

JGH said:
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 ()
{ ... }
}

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
 
A

Alan Krueger

foo said:
Use a static variable -- all instances will then share the
same connection.

JDBC database connections are (IIRC) not thread-safe, so sharing one
among all theads is probably not a good idea.
 
A

anonymous

JGH said:
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?
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
 
B

Bryce

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

Good idea or no? Any better ideas?

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();
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top