1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.transport.jdbc.sqlstrategy; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleEvent; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
13 | |
import org.mule.api.transaction.Transaction; |
14 | |
import org.mule.transaction.TransactionCoordination; |
15 | |
import org.mule.transport.jdbc.JdbcConnector; |
16 | |
import org.mule.transport.jdbc.JdbcUtils; |
17 | |
import org.mule.util.ArrayUtils; |
18 | |
|
19 | |
import java.sql.Connection; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.log4j.Logger; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class SelectSqlStatementStrategy implements SqlStatementStrategy |
30 | |
{ |
31 | 0 | protected transient Logger logger = Logger.getLogger(getClass()); |
32 | |
|
33 | |
public MuleMessage executeStatement(JdbcConnector connector, ImmutableEndpoint endpoint, |
34 | |
MuleEvent event, long timeout) throws Exception |
35 | |
{ |
36 | 0 | logger.debug("Trying to receive a message with a timeout of " + timeout); |
37 | |
|
38 | 0 | String[] stmts = connector.getReadAndAckStatements(endpoint); |
39 | |
|
40 | |
|
41 | 0 | String readStmt = stmts[0]; |
42 | 0 | String ackStmt = stmts[1]; |
43 | |
|
44 | |
|
45 | 0 | List readParams = new ArrayList(); |
46 | 0 | List ackParams = new ArrayList(); |
47 | |
|
48 | |
|
49 | 0 | readStmt = connector.parseStatement(readStmt, readParams); |
50 | 0 | ackStmt = connector.parseStatement(ackStmt, ackParams); |
51 | |
|
52 | 0 | Connection con = null; |
53 | 0 | long t0 = System.currentTimeMillis(); |
54 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
55 | |
try |
56 | |
{ |
57 | 0 | con = connector.getConnection(); |
58 | |
|
59 | |
|
60 | |
|
61 | 0 | if (timeout < 0) |
62 | |
{ |
63 | 0 | timeout = Long.MAX_VALUE; |
64 | |
} |
65 | |
Object result; |
66 | |
|
67 | |
|
68 | |
do |
69 | |
{ |
70 | |
|
71 | 0 | Object[] params = connector.getParams(endpoint, readParams, |
72 | |
event != null ? event.getMessage() : null, |
73 | |
endpoint.getEndpointURI().getAddress()); |
74 | |
|
75 | 0 | if (logger.isDebugEnabled()) |
76 | |
{ |
77 | 0 | logger.debug("SQL QUERY: " + readStmt + ", params = " + ArrayUtils.toString(params)); |
78 | |
} |
79 | |
|
80 | |
|
81 | 0 | result = connector.getQueryRunnerFor(endpoint).query(con, readStmt, |
82 | |
connector.getResultSetHandler(), params); |
83 | |
|
84 | 0 | if (result != null) |
85 | |
{ |
86 | 0 | if (logger.isDebugEnabled()) |
87 | |
{ |
88 | 0 | logger.debug("SQL query received a result: " + result); |
89 | |
} |
90 | 0 | else if (logger.isInfoEnabled()) |
91 | |
{ |
92 | 0 | logger.info("SQL query received a result"); |
93 | |
} |
94 | |
break; |
95 | |
} |
96 | 0 | long sleep = Math.min(connector.getPollingFrequency(), |
97 | |
timeout - (System.currentTimeMillis() - t0)); |
98 | 0 | if (sleep > 0) |
99 | |
{ |
100 | 0 | if (logger.isDebugEnabled()) |
101 | |
{ |
102 | 0 | logger.debug("No results, sleeping for " + sleep); |
103 | |
} |
104 | 0 | Thread.sleep(sleep); |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | logger.debug("Timeout"); |
109 | 0 | JdbcUtils.rollbackAndClose(con); |
110 | 0 | return null; |
111 | |
} |
112 | 0 | } while (true); |
113 | |
|
114 | |
|
115 | 0 | if (ackStmt != null) |
116 | |
{ |
117 | 0 | Object[] params = connector.getParams(endpoint, ackParams, |
118 | |
new DefaultMuleMessage(result, (Map)null, connector.getMuleContext()), ackStmt); |
119 | 0 | if (logger.isDebugEnabled()) |
120 | |
{ |
121 | 0 | logger.debug("SQL UPDATE: " + ackStmt + ", params = " + ArrayUtils.toString(params)); |
122 | |
} |
123 | 0 | int nbRows = connector.getQueryRunnerFor(endpoint).update(con, ackStmt, params); |
124 | 0 | if (nbRows != 1) |
125 | |
{ |
126 | 0 | logger.warn("Row count for ack should be 1 and not " + nbRows); |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
|
131 | 0 | MuleMessage message = null; |
132 | 0 | if (event != null) |
133 | |
{ |
134 | 0 | message = new DefaultMuleMessage(result, event.getMessage(), connector.getMuleContext()); |
135 | |
} |
136 | |
else |
137 | |
{ |
138 | 0 | message = new DefaultMuleMessage(result, connector.getMuleContext()); |
139 | |
} |
140 | |
|
141 | |
|
142 | 0 | if (tx == null) |
143 | |
{ |
144 | 0 | JdbcUtils.commitAndClose(con); |
145 | |
} |
146 | |
|
147 | 0 | return message; |
148 | |
} |
149 | 0 | catch (Exception e) |
150 | |
{ |
151 | 0 | if (tx == null) |
152 | |
{ |
153 | 0 | JdbcUtils.rollbackAndClose(con); |
154 | |
} |
155 | 0 | throw e; |
156 | |
} |
157 | |
} |
158 | |
} |