summaryrefslogtreecommitdiffstats
path: root/Master/Reference Architectures and Patterns/hjp5/examples/CachedConnection.java
blob: 4493adb6d9bcf9d7b8bef657fa4440a311860edc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/* CachedConnection.java */

import java.sql.*;
import java.util.*;

public class CachedConnection
{
  private Connection             con;
  private LinkedList<Statement>  cache;
  private int                    stmtcnt;

  public CachedConnection(Connection con)
  {
    this.con     = con;
    this.cache   = new LinkedList<Statement>();
    this.stmtcnt = 0;
  }

  public Statement getStatement()
  throws SQLException
  {
    if (cache.size() <= 0) {
      return con.createStatement();
    } else {
      return cache.poll();
    }
  }

  public void releaseStatement(Statement statement)
  {
    cache.add(statement);
  }
}