1 22 package org.jboss.ejb.txtimer; 23 24 26 import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil; 27 import org.jboss.logging.Logger; 28 29 import java.io.ByteArrayInputStream ; 30 import java.io.InputStream ; 31 import java.io.Serializable ; 32 import java.sql.Connection ; 33 import java.sql.PreparedStatement ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.sql.Statement ; 37 import java.sql.Timestamp ; 38 import java.util.ArrayList ; 39 import java.util.Date ; 40 import java.util.List ; 41 42 50 public class OracleDatabasePersistencePlugin extends GeneralPurposeDatabasePersistencePlugin 51 { 52 private static Logger log = Logger.getLogger(OracleDatabasePersistencePlugin.class); 54 55 56 public void insertTimer(String timerId, TimedObjectId timedObjectId, Date initialExpiration, long intervalDuration, Serializable info) 57 throws SQLException 58 { 59 Connection con = null; 60 PreparedStatement st = null; 61 try 62 { 63 con = ds.getConnection(); 64 65 String sql = "insert into " + getTableName() + " " + 66 "(" + getColumnTimerID() + "," + getColumnTargetID() + "," + getColumnInitialDate() + "," + getColumnTimerInterval() + "," + getColumnInstancePK() + "," + getColumnInfo() + ") " + 67 "values (?,?,?,?,?,?)"; 68 st = con.prepareStatement(sql); 69 70 st.setString(1, timerId); 71 st.setString(2, timedObjectId.toString()); 72 st.setTimestamp(3, new Timestamp (initialExpiration.getTime())); 73 st.setLong(4, intervalDuration); 74 75 byte[] pkArr = serialize(timedObjectId.getInstancePk()); 76 if (pkArr != null) 77 { 78 InputStream is = new ByteArrayInputStream (pkArr); 79 st.setBinaryStream(5, is, pkArr.length); 80 } 81 else 82 { 83 st.setBytes(5, null); 84 } 85 86 byte[] infoArr = serialize(info); 87 if (infoArr != null) 88 { 89 InputStream is = new ByteArrayInputStream (infoArr); 90 st.setBinaryStream(6, is, infoArr.length); 91 } 92 else 93 { 94 st.setBytes(6, null); 95 } 96 97 int rows = st.executeUpdate(); 98 if (rows != 1) 99 log.error("Unable to insert timer for: " + timedObjectId); 100 } 101 finally 102 { 103 JDBCUtil.safeClose(st); 104 JDBCUtil.safeClose(con); 105 } 106 } 107 108 111 public List selectTimers() 112 throws SQLException 113 { 114 Connection con = null; 115 Statement st = null; 116 ResultSet rs = null; 117 try 118 { 119 con = ds.getConnection(); 120 121 List list = new ArrayList (); 122 123 st = con.createStatement(); 124 rs = st.executeQuery("select * from " + getTableName()); 125 while (rs.next()) 126 { 127 String timerId = rs.getString(getColumnTimerID()); 128 TimedObjectId targetId = TimedObjectId.parse(rs.getString(getColumnTargetID())); 129 Date initialDate = rs.getTimestamp(getColumnInitialDate()); 130 long interval = rs.getLong(getColumnTimerInterval()); 131 132 InputStream isPk = rs.getBinaryStream(getColumnInstancePK()); 133 Serializable pKey = (Serializable )deserialize(isPk); 134 InputStream isInfo = rs.getBinaryStream(getColumnInfo()); 135 Serializable info = (Serializable )deserialize(isInfo); 136 137 targetId = new TimedObjectId(targetId.getContainerId(), pKey); 138 TimerHandleImpl handle = new TimerHandleImpl(timerId, targetId, initialDate, interval, info); 139 list.add(handle); 140 } 141 142 return list; 143 } 144 finally 145 { 146 JDBCUtil.safeClose(rs); 147 JDBCUtil.safeClose(st); 148 JDBCUtil.safeClose(con); 149 } 150 } 151 } 152 153 | Popular Tags |