@@ -79,6 +79,7 @@ private void reconnect() throws SQLException {
79
79
80
80
logInsert = connection .prepareStatement ("insert into log (message_date, message_id, source, message) values (?, ?, ?, ?)" );
81
81
logInsertAttribute = connection .prepareStatement ("insert into log_attribute (message_id, name, value) values (?, ?, ?)" );
82
+ connection .setAutoCommit (false );
82
83
}
83
84
84
85
@ Override
@@ -136,40 +137,44 @@ public void write(Message msg) throws Exception {
136
137
reconnect ();
137
138
}
138
139
139
- connection .setAutoCommit (false );
140
- try {
141
- int index = 1 ;
142
- logInsert .setTimestamp (index ++, new Timestamp (msg .getTimestamp ().getMillis ()));
143
- logInsert .setString (index ++, msg .getId ());
144
- logInsert .setString (index ++, msg .getSource ());
145
- logInsert .setString (index ++, msg .getMessage ());
146
- logInsert .execute ();
147
- Object id = null ;
148
- ResultSet ids = logInsert .getGeneratedKeys ();
149
- while (ids != null && ids .next ()) {
150
- id = ids .getObject (1 );
151
- }
152
- if (id != null ) {
153
- for (Entry <String , Object > e : msg .getFieldsEntries ()) {
154
- String name = e .getKey ();
155
- Object value = e .getValue ();
156
- logInsertAttribute .setObject (1 , id );
157
- logInsertAttribute .setString (2 , name );
158
- logInsertAttribute .setObject (3 , value );
159
- logInsertAttribute .execute ();
160
- }
161
- } else {
162
- throw new SQLException ("Failed to generate ID for primary log record!" );
163
- }
164
- } finally {
165
- connection .rollback ();
166
- connection .commit ();
167
- connection .setAutoCommit (true );
168
- }
140
+ int index = 1 ;
141
+ logInsert .setTimestamp (index ++, new Timestamp (msg .getTimestamp ().getMillis ()));
142
+ logInsert .setString (index ++, msg .getId ());
143
+ logInsert .setString (index ++, msg .getSource ());
144
+ logInsert .setString (index ++, msg .getMessage ());
145
+ logInsert .execute ();
146
+ Object id = null ;
147
+ ResultSet ids = logInsert .getGeneratedKeys ();
148
+ while (ids != null && ids .next ()) {
149
+ id = ids .getObject (1 );
150
+ }
151
+ if (id != null ) {
152
+ for (Entry <String , Object > e : msg .getFieldsEntries ()) {
153
+ String name = e .getKey ();
154
+ Object value = e .getValue ();
155
+ String s = value != null ? value .toString () : null ;
156
+ logInsertAttribute .setObject (1 , id );
157
+ logInsertAttribute .setString (2 , name );
158
+ logInsertAttribute .setString (3 , s );
159
+ logInsertAttribute .execute ();
160
+ }
161
+ } else {
162
+ throw new SQLException ("Failed to generate ID for primary log record!" );
163
+ }
169
164
} catch (SQLException e ) {
170
165
log .log (Level .WARNING , "JDBC output error: " + e .getMessage (), e );
166
+ try {
167
+ connection .rollback ();
168
+ connection .setAutoCommit (true );
169
+ } catch (SQLException ee ) {
170
+ // Don`t care
171
+ }
171
172
connection = null ;
172
- }
173
+ } finally {
174
+ if (connection != null ) {
175
+ connection .commit ();
176
+ }
177
+ }
173
178
}
174
179
175
180
public interface Factory extends MessageOutput .Factory <JDBCOutput > {
0 commit comments