001    /*
002    @license.text@
003     */
004    package biz.hammurapi.sql.xml;
005    
006    import java.sql.Connection;
007    import java.sql.SQLException;
008    
009    import org.w3c.dom.Element;
010    import org.w3c.dom.Node;
011    
012    import biz.hammurapi.config.ConfigurationException;
013    import biz.hammurapi.config.Context;
014    import biz.hammurapi.config.PropertyParser;
015    import biz.hammurapi.sql.SQLProcessor;
016    
017    
018    /**
019     * @author Pavel Vlasov
020     * @version $Revision: 1.2 $
021     */
022    public class Transaction extends CompositeCommand {
023    
024            private String errorMessage;
025    
026            public void configure(Node configNode, Context context) throws ConfigurationException {
027                    super.configure(configNode, context);
028                    Element configElement=(Element) configNode;
029                    
030                    if (configElement.hasAttribute("error-message")) {
031                            errorMessage=configElement.getAttribute("error-message");
032                    }
033                    
034            }
035            
036            public void execute(Element holder, SQLProcessor processor, final Context context) throws SQL2XMLException {
037                    try {
038                            Connection connection = processor.getConnection();
039                            boolean autoCommit=connection.getAutoCommit();
040                            try {
041                                    connection.setAutoCommit(false);
042                                    super.execute(holder, new SQLProcessor(connection, null), context);
043                                    connection.commit();
044                            } catch (SQLException e) {
045                                    connection.rollback();
046                                    throw e;
047                            } catch (SQL2XMLException e) {
048                                    connection.rollback();
049                                    throw e;
050                            } catch (RuntimeException e) {
051                                    connection.rollback();
052                                    throw e;
053                            } finally {
054                                    connection.setAutoCommit(autoCommit);
055                                    connection.close();
056                            }
057                    } catch (final SQLException e) {
058                            throw new SQL2XMLException(
059                                            new PropertyParser(
060                                                            new Context() {
061                                                                    public Object get(String name) {
062                                                                            if ("error.message".equals(name)) {
063                                                                                    return e.getMessage();
064                                                                            } else if ("error.class".equals(name)) {
065                                                                                    return e.getClass().getName();
066                                                                            } else {
067                                                                                    return context.get(name);
068                                                                            }
069                                                                    }
070                                                            }, false).parse(errorMessage), e);
071                    }               
072            }
073    }