You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by mo...@activehotels.com on 2003/12/19 17:30:45 UTC

[DBCP]Rollback doesn't work

Hi!

I have a simple webapp that allows users to register. The user is inserted
in the DB and a confirmation mail is sent to the user.
If the mail fails I'd like the DB to rollback the transaction, but it
doesn't do it. The new user entry is kept in the DB.
I'm using Tomcat 4.1 and mySQL 2.3.2.

I include the code, log and server.xml.

Thanks for your help.

Monica


***CODE:

        Connection dbCon = null;
        boolean isSuccess = false;
        try {
            //inser user in db
            dbCon = new DBUtil().getDBConnection();
            dbCon.setAutoCommit(false);
            userDBPopulator.insertEntry(user, dbCon);

            //send mail to user
            [snip]
            isSuccess = true;
        }
        //rollback  if fail
        catch (MailException e) {
            log.error("MailException " + e.getMessage(), e);
            log.debug("trying to rollback and close");
            try {
                dbCon.rollback();
            }
            catch (SQLException e1) {
                log.error("SQLException rollbacking " + e1.getMessage(), e1);
            }
            try {
                dbCon.close();
            }
            catch (SQLException e1) {
                log.error("SQLException closing connection to DB " +
e1.getMessage(), e1);
            }
        }
        catch (SQLException e) {
            log.error("SQLException " + e.getMessage(), e);
            try {
                dbCon.rollback();
            }
            catch (SQLException e1) {
                log.error("SQLException rollbacking " + e1.getMessage(), e1);
            }
            try {
                dbCon.close();
            }
            catch (SQLException e1) {
                log.error("SQLException closing connection to DB " +
e1.getMessage(), e1);
            }
            throw new ServletException(e);
        }

        //commit user entry
        if( isSuccess){
            log.debug("commiting");
            try {
                dbCon.commit();
            }
            catch (SQLException e1) {
                log.error("SQLException commiting " + e1.getMessage(), e1);
                throw new ServletException("SQLException commiting " +
e1.getMessage(), e1);
            }
            try {
                dbCon.close();
            }
            catch (SQLException e1) {
                log.error("SQLException closing connection to DB " +
e1.getMessage(), e1);
            }
        }

****LOG:

2003-12-19 15:44:12,546 DEBUG [Thread-3] (RegisterServlet.java:200) -
handle register request
2003-12-19 15:44:12,562  INFO [Thread-3] (UserDBReader.java:202) - No user
found with name monica
2003-12-19 15:44:12,578 DEBUG [Thread-3] (MailHelper.java:88) - Sending
mail to m
2003-12-19 15:44:12,625 ERROR [Thread-3] (MailHelper.java:121) - Messaging
Exception: Sending failed
2003-12-19 15:44:12,625 ERROR [Thread-3] (MailHelper.java:124) - Next
Messaging Exception: Invalid Addresses;
2003-12-19 15:44:12,640 DEBUG [Thread-3] (RegisterServlet.java:282) -
trying to rollback and close


***SERVER.XML

	    <Resource name="jdbc/allukmasterDB"
		auth="Container"
		type="javax.sql.DataSource"/>

		<ResourceParams name="jdbc/allukmasterDB">
		    <parameter>
		        <name>factory</name>
		        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
		    </parameter>
		    <parameter>
		        <name>maxActive</name>
		        <value>100</value>
		    </parameter>
		    <parameter>
		        <name>maxIdle</name>
		        <value>30</value>
		    </parameter>
		    <parameter>
		        <name>maxWait</name>
		        <value>10000</value>
		    </parameter>
		    <parameter>
		        <name>username</name>
		        <value>*******</value>
		    </parameter>
		    <parameter>
		        <name>password</name>
		        <value>********</value>
		    </parameter>
		    <parameter>
		        <name>driverClassName</name>
		        <value>com.mysql.jdbc.Driver</value>
		    </parameter>
		    <parameter>
		        <name>url</name>
		        <value>jdbc:mysql://myMachine.com/myDB?autoReconnect=true</value>
		    </parameter>
            <parameter>
                <name>removeAbandoned</name>
                <value>true</value>
            </parameter>
            <parameter>
                <name>logAbandoned</name>
                <value>true</value>
            </parameter>
		</ResourceParams>

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DBCP]Rollback doesn't work

Posted by John Zoetebier <jo...@transparent.co.nz>.
Monica,

Some comments:
- Do a similar rollback test outside the web container and without sending 
email.
	This will tell you if there is a problem with your logic or web container.
- Normally in a web container you:
	Create InitialContext
	Lookup dataSource resource "jdbc/allukmasterDB"
	Get connection from dataSource.getConnection()
- Why do you set flag isSuccess ???
	Remove the flag and replace by commit
	Working with flags for program logic is usualy a sign of bad program 
structure.
	With a try /catch block you don't need flags.

-- 
John Zoetebier
Web site: http://www.transparent.co.nz


On Fri, 19 Dec 2003 16:30:45 -0000 (GMT), <mo...@activehotels.com> wrote:

> Hi!
>
> I have a simple webapp that allows users to register. The user is 
> inserted
> in the DB and a confirmation mail is sent to the user.
> If the mail fails I'd like the DB to rollback the transaction, but it
> doesn't do it. The new user entry is kept in the DB.
> I'm using Tomcat 4.1 and mySQL 2.3.2.
>
> I include the code, log and server.xml.
>
> Thanks for your help.
>
> Monica
>
>
> ***CODE:
>
>         Connection dbCon = null;
>         boolean isSuccess = false;
>         try {
>             //inser user in db
>             dbCon = new DBUtil().getDBConnection();
>             dbCon.setAutoCommit(false);
>             userDBPopulator.insertEntry(user, dbCon);
>
>             //send mail to user
>             [snip]
>             isSuccess = true;
>         }
>         //rollback  if fail
>         catch (MailException e) {
>             log.error("MailException " + e.getMessage(), e);
>             log.debug("trying to rollback and close");
>             try {
>                 dbCon.rollback();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException rollbacking " + e1.getMessage(), 
> e1);
>             }
>             try {
>                 dbCon.close();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException closing connection to DB " +
> e1.getMessage(), e1);
>             }
>         }
>         catch (SQLException e) {
>             log.error("SQLException " + e.getMessage(), e);
>             try {
>                 dbCon.rollback();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException rollbacking " + e1.getMessage(), 
> e1);
>             }
>             try {
>                 dbCon.close();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException closing connection to DB " +
> e1.getMessage(), e1);
>             }
>             throw new ServletException(e);
>         }
>
>         //commit user entry
>         if( isSuccess){
>             log.debug("commiting");
>             try {
>                 dbCon.commit();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException commiting " + e1.getMessage(), 
> e1);
>                 throw new ServletException("SQLException commiting " +
> e1.getMessage(), e1);
>             }
>             try {
>                 dbCon.close();
>             }
>             catch (SQLException e1) {
>                 log.error("SQLException closing connection to DB " +
> e1.getMessage(), e1);
>             }
>         }
>
> ****LOG:
>
> 2003-12-19 15:44:12,546 DEBUG [Thread-3] (RegisterServlet.java:200) -
> handle register request
> 2003-12-19 15:44:12,562  INFO [Thread-3] (UserDBReader.java:202) - No 
> user
> found with name monica
> 2003-12-19 15:44:12,578 DEBUG [Thread-3] (MailHelper.java:88) - Sending
> mail to m
> 2003-12-19 15:44:12,625 ERROR [Thread-3] (MailHelper.java:121) - 
> Messaging
> Exception: Sending failed
> 2003-12-19 15:44:12,625 ERROR [Thread-3] (MailHelper.java:124) - Next
> Messaging Exception: Invalid Addresses;
> 2003-12-19 15:44:12,640 DEBUG [Thread-3] (RegisterServlet.java:282) -
> trying to rollback and close
>
>
> ***SERVER.XML
>
> 	    <Resource name="jdbc/allukmasterDB"
> 		auth="Container"
> 		type="javax.sql.DataSource"/>
>
> 		<ResourceParams name="jdbc/allukmasterDB">
> 		    <parameter>
> 		        <name>factory</name>
> 		        <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>maxActive</name>
> 		        <value>100</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>maxIdle</name>
> 		        <value>30</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>maxWait</name>
> 		        <value>10000</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>username</name>
> 		        <value>*******</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>password</name>
> 		        <value>********</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>driverClassName</name>
> 		        <value>com.mysql.jdbc.Driver</value>
> 		    </parameter>
> 		    <parameter>
> 		        <name>url</name>
> 		        
> <value>jdbc:mysql://myMachine.com/myDB?autoReconnect=true</value>
> 		    </parameter>
>             <parameter>
>                 <name>removeAbandoned</name>
>                 <value>true</value>
>             </parameter>
>             <parameter>
>                 <name>logAbandoned</name>
>                 <value>true</value>
>             </parameter>
> 		</ResourceParams>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org