fandyvon / activejdbc

Automatically exported from code.google.com/p/activejdbc
0 stars 0 forks source link

Transaction support Is not working #197

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Attached the code below. I do a save on employee1 but error occurs when i 
try to do a save on employee2 since i pass string to an integer filed. 
2. Since i have not committed the transaction i dont expect to see the 
employee1 record in db. However when i query the database i see that employee1 
record is present in db.

What is the expected output? What do you see instead?
If i do a saveIt on one object and saveIt fails on the second 
object,rollbackTransaction should undo any inserts to db.

What version of the product are you using? On what operating system?
Linux 64 bit, ubuntu, jdk 6.
<dependency>
            <groupId>org.javalite</groupId>
            <artifactId>activejdbc</artifactId>
            <version>1.4.8</version>
        </dependency>

Please provide any additional information below.

package com.reardencommerce.tools.activejdbc;

import java.util.logging.Logger;
import javax.sql.DataSource;
import org.javalite.activejdbc.Base;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App {

    public static final Logger log = Logger.getLogger(App.class.getName());

    public static void main(String[] args) {
        new App().dbCall();
    }

    public void dbCall() {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        //Base.open(dataSource);
        Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/simple_test", "root", "pwd");
        this.addEmployee("Jhon", "Doe");
    }

    public void addEmployee(String firstName, String lastName) {
        try {
            Base.connection().setAutoCommit(false);

            Base.openTransaction();
            Employee e1 = new Employee();
            e1.set(Employee.FIRST_NAME, firstName);
            e1.set(Employee.LAST_NAME, lastName);
            e1.set(Employee.SALARY, 1);
            e1.saveIt();

            Employee e2 = new Employee();
            e2.set(Employee.FIRST_NAME, firstName);
            e2.set(Employee.LAST_NAME, lastName);
            e2.set(Employee.SALARY, "A");
            e2.saveIt();

            Base.commitTransaction();

        } catch (Exception ex) {
            log.severe(ex.getMessage());
            Base.rollbackTransaction();
        } finally {
            Base.close();

        }

    }
}

Original issue reported on code.google.com by arjun.su...@gmail.com on 25 Oct 2013 at 11:52

GoogleCodeExporter commented 9 years ago
Looks like you are using MySQL. In that case, ensure you are using InnoDB 
tables engine, and not MyISAM, since MyISAM does not support transactions. 

Original comment by i...@expresspigeon.com on 28 Oct 2013 at 10:22

GoogleCodeExporter commented 9 years ago
Yes you were right. I had failed to notice that. It works when i change over to 
innodb. 

I am now trying to integrate with spring transaction manager. However unless i 
do an explicit commit transaction the inserts are not happening. Spring 
transaction manager should have take care of the commit.

public class App {

    public static final Logger log = Logger.getLogger(App.class.getName());

    public static void main(String[] args) throws Exception {
        new App().dbCall();
    }

    public void dbCall() throws Exception {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");
        Base.open(dataSource);
        Base.connection().setAutoCommit(false);
        this.addEmployee("Jhon", "Doe");
        Base.close();
    }

    @Transactional
    public void addEmployee(String firstName, String lastName) {

        Employee e1 = new Employee();
        e1.set(Employee.FIRST_NAME, firstName);
        e1.set(Employee.LAST_NAME, lastName);
        e1.set(Employee.SALARY, 1);
        e1.saveIt();

        Employee e2 = new Employee();
        e2.set(Employee.FIRST_NAME, firstName);
        e2.set(Employee.LAST_NAME, lastName);
        e2.set(Employee.SALARY, 2);
        e2.saveIt();

    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config/>
    <tx:annotation-driven/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/simple_test" />
        <property name="username" value="root" />        
        <property name="password" value="manager" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

Original comment by arjun.su...@gmail.com on 30 Oct 2013 at 5:39

GoogleCodeExporter commented 9 years ago
I'm rejecting this issue. Going forward, please post questions to Google Groups:
https://groups.google.com/forum/#!forum/activejdbc-group
and file issues on Githug under corresponding repo: https://github.com/javalite

As far as Spring, please, see that ActiveJDBC does not really manage 
transactions.
Please see code here: 
https://github.com/javalite/activejdbc/blob/master/activejdbc/src/main/java/org/
javalite/activejdbc/DB.java#L570

and here: 
http://javalt.org/p/transactions

So, maybe this question needs to be posted to Spring forum? 
I personally never use Spring. 

tx

Original comment by i...@polevoy.org on 30 Oct 2013 at 6:19