package org.jboss.ejb.txtimer;
import org.jboss.ejb.plugins.cmp.jdbc.JDBCUtil;
import org.jboss.logging.Logger;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class OracleDatabasePersistencePlugin extends GeneralPurposeDatabasePersistencePlugin
{
private static Logger log = Logger.getLogger(OracleDatabasePersistencePlugin.class);
public void insertTimer(String timerId, TimedObjectId timedObjectId, Date initialExpiration, long intervalDuration, Serializable info)
throws SQLException
{
Connection con = null;
PreparedStatement st = null;
try
{
con = ds.getConnection();
String sql = "insert into " + getTableName() + " " +
"(" + getColumnTimerID() + "," + getColumnTargetID() + "," + getColumnInitialDate() + "," + getColumnTimerInterval() + "," + getColumnInstancePK() + "," + getColumnInfo() + ") " +
"values (?,?,?,?,?,?)";
st = con.prepareStatement(sql);
st.setString(1, timerId);
st.setString(2, timedObjectId.toString());
st.setTimestamp(3, new Timestamp(initialExpiration.getTime()));
st.setLong(4, intervalDuration);
byte[] pkArr = serialize(timedObjectId.getInstancePk());
if (pkArr != null)
{
InputStream is = new ByteArrayInputStream(pkArr);
st.setBinaryStream(5, is, pkArr.length);
}
else
{
st.setBytes(5, null);
}
byte[] infoArr = serialize(info);
if (infoArr != null)
{
InputStream is = new ByteArrayInputStream(infoArr);
st.setBinaryStream(6, is, infoArr.length);
}
else
{
st.setBytes(6, null);
}
int rows = st.executeUpdate();
if (rows != 1)
log.error("Unable to insert timer for: " + timedObjectId);
}
finally
{
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
public List selectTimers()
throws SQLException
{
Connection con = null;
Statement st = null;
ResultSet rs = null;
try
{
con = ds.getConnection();
List list = new ArrayList();
st = con.createStatement();
rs = st.executeQuery("select * from " + getTableName());
while (rs.next())
{
String timerId = rs.getString(getColumnTimerID());
TimedObjectId targetId = TimedObjectId.parse(rs.getString(getColumnTargetID()));
Date initialDate = rs.getTimestamp(getColumnInitialDate());
long interval = rs.getLong(getColumnTimerInterval());
InputStream isPk = rs.getBinaryStream(getColumnInstancePK());
Serializable pKey = (Serializable)deserialize(isPk);
InputStream isInfo = rs.getBinaryStream(getColumnInfo());
Serializable info = (Serializable)deserialize(isInfo);
targetId = new TimedObjectId(targetId.getContainerId(), pKey);
TimerHandleImpl handle = new TimerHandleImpl(timerId, targetId, initialDate, interval, info);
list.add(handle);
}
return list;
}
finally
{
JDBCUtil.safeClose(rs);
JDBCUtil.safeClose(st);
JDBCUtil.safeClose(con);
}
}
}