1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.transport.jdbc.sqlstrategy; |
12 | |
|
13 | |
import org.mule.DefaultMuleMessage; |
14 | |
import org.mule.api.MuleEvent; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
17 | |
import org.mule.api.transaction.Transaction; |
18 | |
import org.mule.transaction.TransactionCoordination; |
19 | |
import org.mule.transport.jdbc.JdbcConnector; |
20 | |
import org.mule.transport.jdbc.JdbcUtils; |
21 | |
import org.mule.util.ArrayUtils; |
22 | |
|
23 | |
import java.sql.Connection; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.log4j.Logger; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | 0 | public class SimpleUpdateSqlStatementStrategy implements SqlStatementStrategy |
33 | |
{ |
34 | 0 | protected transient Logger logger = Logger.getLogger(getClass()); |
35 | |
|
36 | |
public MuleMessage executeStatement(JdbcConnector connector, |
37 | |
ImmutableEndpoint endpoint, MuleEvent event,long timeout) throws Exception |
38 | |
{ |
39 | |
|
40 | 0 | String statement = connector.getStatement(endpoint); |
41 | |
|
42 | |
|
43 | 0 | List paramNames = new ArrayList(); |
44 | |
|
45 | |
|
46 | 0 | String sql = connector.parseStatement(statement, paramNames); |
47 | |
|
48 | |
|
49 | 0 | sql = escapeStatement(sql); |
50 | |
|
51 | |
|
52 | 0 | MuleMessage message = event.getMessage(); |
53 | 0 | Object[] paramValues = connector.getParams(endpoint, paramNames, new DefaultMuleMessage( |
54 | |
event.getMessage().getPayload(), message, event.getMuleContext()), endpoint.getEndpointURI().getAddress()); |
55 | |
|
56 | 0 | Transaction tx = TransactionCoordination.getInstance().getTransaction(); |
57 | 0 | Connection con = null; |
58 | |
|
59 | |
try |
60 | |
{ |
61 | 0 | con = connector.getConnection(); |
62 | |
|
63 | |
|
64 | 0 | if (logger.isDebugEnabled()) |
65 | |
{ |
66 | 0 | logger.debug("SQL UPDATE: " + sql + ", params = " + ArrayUtils.toString(paramValues)); |
67 | |
} |
68 | |
|
69 | 0 | int nbRows = connector.getQueryRunnerFor(endpoint).update(con, sql, paramValues); |
70 | 0 | if (logger.isInfoEnabled()) |
71 | |
{ |
72 | 0 | logger.info("Executing SQL statement: " + nbRows + " row(s) updated"); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | 0 | if (tx == null) |
83 | |
{ |
84 | 0 | JdbcUtils.commitAndClose(con); |
85 | |
} |
86 | 0 | logger.debug("MuleEvent dispatched succesfuly"); |
87 | |
} |
88 | 0 | catch (Exception e) |
89 | |
{ |
90 | 0 | logger.debug("Error dispatching event: " + e.getMessage(), e); |
91 | 0 | if (tx == null) |
92 | |
{ |
93 | 0 | JdbcUtils.rollbackAndClose(con); |
94 | |
} |
95 | 0 | throw e; |
96 | 0 | } |
97 | |
|
98 | 0 | return event.getMessage(); |
99 | |
} |
100 | |
|
101 | |
protected String escapeStatement(String statement) |
102 | |
{ |
103 | |
|
104 | 0 | return statement; |
105 | |
} |
106 | |
} |