Skip to content

Commit 02faee5

Browse files
author
Peter Mount
committed
ImageViewer transaction fixes
1 parent 2937bf0 commit 02faee5

File tree

3 files changed

+66
-40
lines changed

3 files changed

+66
-40
lines changed

src/interfaces/jdbc/CHANGELOG

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
Wed May 02 16:47:00 BST 2000 petermount@it.maidstone.gov.uk
1+
Thu May 04 11:38:00 BST 2000 petermount@it.maidstone.gov.uk
2+
- Corrected incorrect date in CHANGELOG
3+
- Fixed the ImageViewer example
4+
5+
Wed May 03 16:47:00 BST 2000 petermount@it.maidstone.gov.uk
26
- Fixed the Makefile so that postgresql.jar is built everytime
37
the jdbc1 or jdbc2 rules are called.
48
- Fixed the threadsafe example. It had problems with autocommit
59

6-
Wed May 02 14:32:00 BST 2000 petermount@it.maidstone.gov.uk
10+
Wed May 03 14:32:00 BST 2000 petermount@it.maidstone.gov.uk
711
- Rewrote the README file (the old one was 18 months old!)
812
- Added @deprecated tags to org.postgresql.jdbc2.ResultSet
913
to clear some warnings issued during compilation.

src/interfaces/jdbc/Makefile

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# Makefile for Java JDBC interface
55
#
66
# IDENTIFICATION
7-
# $Id: Makefile,v 1.20 2000/05/03 15:58:08 peter Exp $
7+
# $Id: Makefile,v 1.21 2000/05/05 07:35:29 peter Exp $
88
#
99
#-------------------------------------------------------------------------
1010

@@ -68,10 +68,14 @@ msg:
6868
@echo
6969
@echo ------------------------------------------------------------
7070
@echo To build the examples, type:
71-
@echo " make examples"
71+
@echo "JDBC1: make examples"
72+
@echo "JDBC2: make examples2"
7273
@echo
7374
@echo "To build the CORBA example (requires Java2):"
7475
@echo " make corba"
76+
@echo
77+
@echo "To make the tests, type:"
78+
@echo " make tests"
7579
@echo ------------------------------------------------------------
7680
@echo
7781

@@ -214,12 +218,18 @@ $(PGBASE)/util/UnixCrypt.class: $(PGBASE)/util/UnixCrypt.java
214218
#######################################################################
215219
# These classes are in the example directory, and form the examples
216220
EX= example/basic.class \
217-
example/blobtest.class \
218-
example/datestyle.class \
219221
example/psql.class \
220-
example/ImageViewer.class \
221-
example/metadata.class \
222+
example/ImageViewer.class
223+
224+
# These are only valid for JDBC2
225+
EX2= example/blobtest.class
226+
227+
# These are really test classes not true examples
228+
TESTS= example/metadata.class \
222229
example/threadsafe.class
230+
231+
# Non functional/obsolete examples
232+
# example/datestyle.class \
223233
# example/Objects.class
224234

225235
# This rule builds the examples
@@ -229,20 +239,31 @@ examples: postgresql.jar $(EX)
229239
@echo
230240
@echo For instructions on how to use them, simply run them. For example:
231241
@echo
232-
@echo " java example.blobtest"
242+
@echo " java example.basic"
233243
@echo
234244
@echo This would display instructions on how to run the example.
235245
@echo ------------------------------------------------------------
236246
@echo Available examples:
237247
@echo
238248
@echo " example.basic Basic JDBC useage"
239-
@echo " example.blobtest Binary Large Object tests"
240249
@echo " example.datestyle Shows how datestyles are handled"
241250
@echo " example.ImageViewer Example application storing images"
242251
@echo " example.psql Simple java implementation of psql"
243-
@echo " example.Objects Demonstrates Object Serialisation"
244252
@echo " "
245-
@echo These are not really examples, but tests various parts of the driver
253+
@echo ------------------------------------------------------------
254+
@echo
255+
256+
examples2: $(EX2) examples
257+
@echo "The following JDBC2 only examples have also been built:"
258+
@echo
259+
@echo " example.blobtest Binary Large Object tests"
260+
@echo
261+
@echo ------------------------------------------------------------
262+
@echo
263+
264+
tests: $(TESTS)
265+
@echo ------------------------------------------------------------
266+
@echo The following tests have been built:
246267
@echo " example.metadata Tests various metadata methods"
247268
@echo " example.threadsafe Tests the driver's thread safety"
248269
@echo ------------------------------------------------------------

src/interfaces/jdbc/example/ImageViewer.java

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,11 @@ public void actionPerformed(ActionEvent e) {
186186
Class.forName("org.postgresql.Driver");
187187

188188
// Connect to database
189-
System.out.println("Connecting to Database URL = " + url);
190189
db = DriverManager.getConnection(url, user, password);
191190

192191
// Create a statement
193192
stat = db.createStatement();
194193

195-
// Set the connection to use transactions
196-
db.setAutoCommit(false);
197-
198194
// Also, get the LargeObjectManager for this connection
199195
lom = ((org.postgresql.Connection)db).getLargeObjectAPI();
200196

@@ -210,7 +206,7 @@ public void actionPerformed(ActionEvent e) {
210206
public void init()
211207
{
212208
try {
213-
db.setAutoCommit(true);
209+
//db.setAutoCommit(true);
214210
stat.executeUpdate("create table images (imgname name,imgoid oid)");
215211
label.setText("Initialised database");
216212
db.commit();
@@ -219,11 +215,11 @@ public void init()
219215
}
220216

221217
// This must run outside the previous try{} catch{} segment
222-
try {
223-
db.setAutoCommit(true);
224-
} catch(SQLException ex) {
225-
label.setText(ex.toString());
226-
}
218+
//try {
219+
//db.setAutoCommit(true);
220+
//} catch(SQLException ex) {
221+
//label.setText(ex.toString());
222+
//}
227223
}
228224

229225
/**
@@ -283,37 +279,29 @@ public void run() {
283279
// fetch the large object manager
284280
LargeObjectManager lom = ((org.postgresql.Connection)db).getLargeObjectAPI();
285281

286-
System.out.println("Importing file");
282+
db.setAutoCommit(false);
283+
287284
// A temporary buffer - this can be as large as you like
288285
byte buf[] = new byte[2048];
289286

290287
// Open the file
291-
System.out.println("Opening file "+dir+"/"+name);
292288
FileInputStream fis = new FileInputStream(new File(dir,name));
293289

294-
// Gain access to large objects
295-
System.out.println("Gaining LOAPI");
296-
297290
// Now create the large object
298-
System.out.println("creating blob");
299291
int oid = lom.create();
300-
301-
System.out.println("Opening "+oid);
302292
LargeObject blob = lom.open(oid);
303293

304294
// Now copy the file into the object.
305295
//
306296
// Note: we dont use write(buf), as the last block is rarely the same
307297
// size as our buffer, so we have to use the amount read.
308-
System.out.println("Importing file");
309298
int s,t=0;
310299
while((s=fis.read(buf,0,buf.length))>0) {
311-
System.out.println("Block s="+s+" t="+t);t+=s;
300+
t+=s;
312301
blob.write(buf,0,s);
313302
}
314303

315304
// Close the object
316-
System.out.println("Closing blob");
317305
blob.close();
318306

319307
// Now store the entry into the table
@@ -323,6 +311,7 @@ public void run() {
323311
stat = db.createStatement();
324312
stat.executeUpdate("insert into images values ('"+name+"',"+oid+")");
325313
db.commit();
314+
db.setAutoCommit(false);
326315

327316
// Finally refresh the names list, and display the current image
328317
ImageViewer.this.refreshList();
@@ -370,26 +359,28 @@ public void refreshList()
370359
public void removeImage()
371360
{
372361
try {
362+
//
373363
// Delete any large objects for the current name
364+
//
365+
// Note: We don't need to worry about being in a transaction
366+
// here, because we are not opening any blobs, only deleting
367+
// them
368+
//
374369
ResultSet rs = stat.executeQuery("select imgoid from images where imgname='"+currentImage+"'");
375370
if(rs!=null) {
376371
// Even though there should only be one image, we still have to
377372
// cycle through the ResultSet
378373
while(rs.next()) {
379-
System.out.println("Got oid "+rs.getInt(1));
380374
lom.delete(rs.getInt(1));
381-
System.out.println("Import complete");
382375
}
383376
}
384377
rs.close();
385378

386379
// Finally delete any entries for that name
387380
stat.executeUpdate("delete from images where imgname='"+currentImage+"'");
388-
db.commit();
389381

390382
label.setText(currentImage+" deleted");
391383
currentImage=null;
392-
db.commit();
393384
refreshList();
394385
} catch(SQLException ex) {
395386
label.setText(ex.toString());
@@ -404,21 +395,30 @@ public void removeImage()
404395
public void displayImage(String name)
405396
{
406397
try {
407-
System.out.println("Selecting oid for "+name);
398+
//
399+
// Now as we are opening and reading a large object we must
400+
// turn on Transactions. This includes the ResultSet.getBytes()
401+
// method when it's used on a field of type oid!
402+
//
403+
db.setAutoCommit(false);
404+
408405
ResultSet rs = stat.executeQuery("select imgoid from images where imgname='"+name+"'");
409406
if(rs!=null) {
410407
// Even though there should only be one image, we still have to
411408
// cycle through the ResultSet
412409
while(rs.next()) {
413-
System.out.println("Got oid "+rs.getInt(1));
414410
canvas.setImage(canvas.getToolkit().createImage(rs.getBytes(1)));
415-
System.out.println("Import complete");
416411
label.setText(currentImage = name);
417412
}
418413
}
419414
rs.close();
420415
} catch(SQLException ex) {
421416
label.setText(ex.toString());
417+
} finally {
418+
try {
419+
db.setAutoCommit(true);
420+
} catch(SQLException ex2) {
421+
}
422422
}
423423
}
424424

@@ -454,6 +454,7 @@ public static void main(String args[])
454454
frame.setLayout(new BorderLayout());
455455
ImageViewer viewer = new ImageViewer(frame,args[0],args[1],args[2]);
456456
frame.pack();
457+
frame.setLocation(0,50);
457458
frame.setVisible(true);
458459
} catch(Exception ex) {
459460
System.err.println("Exception caught.\n"+ex);

0 commit comments

Comments
 (0)