Mysql and transactions..

Any1 has any code showing how this is done using jdbc?
Tnx.

whateverMethodRunsAQuery("WHATEVER QUERY YOU USE TO START A TRANSACTION");

???

http://www.developer.com/java/data/article.php/3417381

I couldn’t find anything thingon transactions in that link.
you asked for http://java.sun.com/docs/books/tutorial/jdbc/basics/transactions.html

http://www.jpox.org/docs/1_2/jpa/transaction_types.html outlines normal ways adleast I wouldn’t want to bother with jdbc (I don’t use JPOX, but that was the first hit google.)

“A Transaction forms a unit of work. The Transaction manages what happens within that unit of work, and when an error occurs the Transaction can roll back any changes performed. Transactions can be managed by the users application, or can be managed by a framework (such as Spring), or can be managed by a J2EE container.”

I thought it had them inside yellow boxes

Tnx for ur responses, i ll check out that first link Mr_Light provided.
Gl to me i guess :slight_smile:

Here’s the standard way to work with transactions in JDBC (assuming you’re not running your code inside a J2EE server):


String driver = "mysql.driver.classname";
Class.forName(driver).newInstance();
String url = "jdbc:mysqlserver:port";

Properties props = new Properties();
props.putProperty("user", "username");
props.putProperty("password", "***");
Connection conn = DriverManager.getConnection(url, props);

//the next line is the real trick to work with transactions:
conn.setAutoCommit(false);
Statement workStm = conn.createStatement(); //this statement will perform all the work

Every statement that is created from the same connection, is part of the same transaction. Different connection = different transactions. Because you set autocommit to false, you’ll need to commit or rollback yourself:


conn.commit();
conn.rollback();

I’ve never done fancy stuff like the savepoints, so I can’t help you with that. :slight_smile:

I ve noticed something like this:


try{
  conn.setAutoCommit(false);
  makeATempTable();
  insertSomethingies();
}catch(SQLException e){
  conn.rollBack();
}

It wont roll back the creation of tmp table. Am i doing something wrong or is it a feature? :slight_smile: Is it supposed to be deleted manually?

I don’t think creation of tables and other meta-data work (like adding/removing columns, locking tables etc.) can be part of a transaction.

  • elias

considering that they are sql statements too they should, my prejuge saids blame mysql but perhaps having a look at the spec first would be more fair.