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