KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > pm > jdbc2 > OracleThinPersistenceManager


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.mq.pm.jdbc2;
23
24 import java.io.IOException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import javax.jms.JMSException JavaDoc;
31
32 import org.jboss.mq.SpyMessage;
33 import org.jboss.mq.pm.Tx;
34
35 /**
36  * OracleThinPersistenceManager.<p>
37  *
38  * Based on information provided by Ron Teeter at TradeBeam Holdings, Inc.
39  *
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @version $Revision: 38724 $
42  */

43 public class OracleThinPersistenceManager extends PersistenceManager
44 {
45    /** Insert an empty blob */
46    protected String JavaDoc INSERT_EMPTY_BLOB = "INSERT INTO JMS_MESSAGES (MESSAGEID, DESTINATION, MESSAGEBLOB, TXID, TXOP) VALUES(?,?,EMPTY_BLOB(),?,?)";
47    /** Lock empty blob */
48    protected String JavaDoc LOCK_EMPTY_BLOB = "SELECT MESSAGEID, MESSAGEBLOB FROM JMS_MESSAGES WHERE MESSAGEID = ? AND DESTINATION = ? FOR UPDATE";
49
50    /**
51     * Create a new OracleThinPersistenceManager.
52     *
53     * @throws JMSException for any error
54     */

55    public OracleThinPersistenceManager() throws JMSException JavaDoc
56    {
57    }
58
59    protected void add(Connection JavaDoc c, String JavaDoc queue, SpyMessage message, Tx txId, String JavaDoc mark) throws SQLException JavaDoc, IOException JavaDoc
60    {
61       PreparedStatement JavaDoc stmt = null;
62       ResultSet JavaDoc rs = null;
63       try
64       {
65          stmt = c.prepareStatement(INSERT_EMPTY_BLOB);
66
67          stmt.setLong(1, message.header.messageId);
68          stmt.setString(2, queue);
69
70          if (txId != null)
71             stmt.setLong(3, txId.longValue());
72          else
73             stmt.setNull(3, java.sql.Types.BIGINT);
74          stmt.setString(4, mark);
75
76          int count = stmt.executeUpdate();
77          safeClose(stmt, null);
78          if (count != 1)
79             throw new IOException JavaDoc("Could not insert empty blob in the database: insert affected " + count + " rows. message=" + message);
80
81          stmt = c.prepareStatement(LOCK_EMPTY_BLOB);
82          stmt.setLong(1, message.header.messageId);
83          stmt.setString(2, queue);
84
85          rs = stmt.executeQuery();
86          if (rs.next() == false)
87             throw new IOException JavaDoc("Could not lock empty blob in the database. message=" + message);
88          safeClose(stmt, rs);
89
90          stmt = c.prepareStatement(UPDATE_MESSAGE);
91          setBlob(stmt, 1, message);
92          stmt.setLong(2, message.header.messageId);
93          stmt.setString(3, queue);
94
95          count = stmt.executeUpdate();
96          safeClose(stmt, null);
97          if (count != 1)
98             throw new IOException JavaDoc("Could not update real blob in the database: update affected " + count + " rows. message=" + message);
99       }
100       finally
101       {
102          safeClose(stmt, rs);
103       }
104    }
105
106    public void startService() throws Exception JavaDoc
107    {
108       INSERT_EMPTY_BLOB = sqlProperties.getProperty("INSERT_EMPTY_BLOB", INSERT_EMPTY_BLOB);
109       LOCK_EMPTY_BLOB = sqlProperties.getProperty("LOCK_EMPTY_BLOB", LOCK_EMPTY_BLOB);
110       super.startService();
111    }
112    
113    protected void safeClose(PreparedStatement JavaDoc stmt, ResultSet JavaDoc rs)
114    {
115       try
116       {
117          if (rs != null)
118          {
119             rs.close();
120          }
121       }
122       catch (SQLException JavaDoc ignored)
123       {
124          log.trace("Ignored", ignored);
125       }
126       try
127       {
128          if (stmt != null)
129          {
130             stmt.close();
131          }
132       }
133       catch (SQLException JavaDoc ignored)
134       {
135          log.trace("Ignored", ignored);
136       }
137    }
138 }
139
Popular Tags