RecordManagerBean.java
01 package trail.injection;
02 
03 import javax.ejb.Stateless;
04 import javax.ejb.TimerService;
05 import javax.ejb.SessionContext;
06 import javax.annotation.Resource;
07 import javax.jms.QueueConnectionFactory;
08 import javax.jms.Queue;
09 import java.sql.*;
10 import javax.sql.*;
11 
12 @Stateless
13 public class RecordManagerBean implements RecordManager {
14 
15   // Inject resources based on global jndi names
16   @Resource (mappedName="java:/DefaultDS")
17   DataSource myDb;
18 
19   @Resource (mappedName="ConnectionFactory")
20   QueueConnectionFactory factory;
21 
22   @Resource (mappedName="queue/testQueue")
23   Queue queue;
24 
25   // Inject resources based on type
26   @Resource
27   TimerService tms;
28 
29   @Resource
30   SessionContext ctx;
31 
32   public void addRecord (long sent, double result) {
33     Connection conn = null;
34     PreparedStatement pstmt = null;
35     try {
36 
37       conn = myDb.getConnection();
38       pstmt = conn.prepareStatement (
39               "INSERT INTO INJECTIONREC " +
40               "(SENT,PROCESSED,RESULT) " +
41               "VALUES (?, ?, ?)");
42       pstmt.setLong (1, sent);
43       long processed = System.currentTimeMillis();
44       pstmt.setLong (2, processed);
45       pstmt.setDouble (3, result);
46       pstmt.executeUpdate();
47 
48     catch (Exception e) {
49       e.printStackTrace ();
50     finally {
51       try {
52         pstmt.close ();
53         conn.close ();
54       catch (Exception e) {
55         e.printStackTrace ();
56       }
57     }
58   }
59 
60   public CalculationRecord getRecord (long sent) {
61     Connection conn = null;
62     Statement stmt = null;
63     ResultSet rs = null;
64 
65     CalculationRecord rc = null;
66 
67     try {
68 
69       conn = myDb.getConnection();
70       stmt = conn.createStatement ();
71       rs = stmt.executeQuery("SELECT * FROM INJECTIONREC");
72 
73       while (rs.next()) {
74         if (rs.getLong("sent"== sent) {
75           rc = new CalculationRecord (
76               rs.getLong ("sent"),
77               rs.getLong ("processed"),
78               rs.getDouble ("result"));
79           break;
80         }
81       }
82 
83     catch (Exception e) {
84       e.printStackTrace ();
85     finally {
86       try {
87         rs.close ();
88         stmt.close ();
89         conn.close ();
90       catch (Exception e) {
91         e.printStackTrace ();
92       }
93     }
94     return rc;
95   }
96 
97 }