Results 1 to 7 of 7

Thread: MYSql Autocommi

  1. #1
    Join Date
    Apr 2005
    Location
    Galway, Ireland
    Posts
    24

    Default MYSql Autocommi

    Hi There,
    Have tried the Hibernate forum on this but no avail. I am trying the following.
    e.g.
    public void saveProduct(Product p) {
    getHibernateTemplate().saveOrUpdate(p);
    }
    Hibernate Mapping File Sample
    Code:
    <class 
        name="Product" 
        table="products"
    >
        <meta attribute="class-description" inherit="false">
           @hibernate.class
            table="products"
        </meta>
    
        <id
            name="id"
            type="java.lang.Integer"
            column="ID"
        >
            <meta attribute="field-description">
               @hibernate.id
                generator-class="native"
                type="java.lang.Integer"
                column="ID"
    
            </meta>
            <generator class="native" />
        </id>
    I get the folllwing error:

    Code:
    Translating SQLException with SQLState 'null' and errorCode '0' and message &#91;Can't call commit when autocommit=true&#93;; SQL was &#91;null&#93; for task &#91;HibernateAccessor&#93;
    I set autcommit in MYSql to be false as follows.


    Code:
    set autocommit=0;
    
    mysql> select @@autocommit;
    +--------------+
    | @@autocommit |
    +--------------+
    |            0     |
    +--------------+
    Any Ideas would be helpful on this one. I can read fine from the datbase its the inserts thats causing me pain.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    How do you configure your connection pool? Pools like Commons DBCP expose autocommit as a property that you can set in the DataSource bean definition in Spring. If you use an application server's pool, autocommit will normally be switched off by default. You should really configure autocommit at connection pool level, not database level.

    Rgds
    Rod
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Apr 2005
    Location
    Galway, Ireland
    Posts
    24

    Default

    Hi Rod,
    Appreciate the prompt reply on this one. its much appreciated.

    Quote Originally Posted by Rod Johnson
    How do you configure your connection pool? Pools like Commons DBCP expose autocommit as a property that you can set in the DataSource bean definition in Spring.
    Code:
      <bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property>
            <property name="url"><value>jdbc&#58;mysql&#58;//localhost/dbname?relaxAutoCommit=true</value></property>
            <property name="username"><value>user</value></property>
            <property name="password"><value>pass</value></property>
        </bean>
    I havnt seen any posts like this out there, which might suggest i am doing something wrong and could possible be the case?

    Thanks in advance.
    Slán

  4. #4
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    Don't use DriverManagerDataSource. Use a connection pool. Switch off autocommit as follows:

    Code:
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
              <property name="driverClassName"><value>org.gjt.mm.mysql.Driver</value></property>
              <property name="url"><value>jdbc&#58;mysql&#58;//localhost/dbname</value></property>
              <property name="username"><value>user</value></property>
              <property name="password"><value>pass</value></property>
             <property name="defaultAutoCommit"><value>false</value></property>
    </bean>
    Note the defaultAutoCommit property of Commons pool, which sets the default autocommit value for all connections in the pool.

    That example uses Apache Commons DBCP, as in Spring samples such as PetStore. You'll need the Commons DBCP JAR on your path. You could equally well use another pool such as C3P0. Refer to its documentation in that case for how to disable autocommit by default.

    With connection pooling you will get much better performance as well.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  5. #5
    Join Date
    Apr 2005
    Location
    Galway, Ireland
    Posts
    24

    Default

    That solved it alright. Thanks for this.
    The data hasnt appeared in the database table yet, but i am getting a value for the Product.getId() so will have a look seee why its not saving to the database when the Junit test finsishes.



    Go raibh mile maith agaibh - (A thousand thanks IRL)

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    The data hasnt appeared in the database table yet, but i am getting a value for the Product.getId() so will have a look seee why its not saving to the database when the Junit test finsishes
    Are you managing transactions anywhere (e.g. declaratively with TransactionProxyFactoryBean)? Otherwise the effect of no autocommit will be no permanently persisted data at all. At least it will be atomic :-)
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  7. #7
    Join Date
    Jun 2007
    Posts
    1

    Default Turning off autocommit for C3P0

    I ran into the same problem (connections being set to autocommit). I could not figure out how to configure C3P0 to serve connections with autocommit=false. Has anyone successfully done this? I had to use Apache Commons DBCP which works fine (thanks to the post on this thread), but would rather go back to C3P0.

Similar Threads

  1. Spring, JDBC and MySQL
    By rlynn in forum Data
    Replies: 4
    Last Post: Aug 19th, 2005, 07:42 AM
  2. Replies: 0
    Last Post: Jun 26th, 2005, 07:34 AM
  3. Problem with JDBC DAO and MySQL
    By johnny2005 in forum Data
    Replies: 7
    Last Post: Jun 21st, 2005, 04:17 PM
  4. ACEGI + CAS SERVER + MYSQL ????
    By lazzha in forum Security
    Replies: 2
    Last Post: Jun 21st, 2005, 04:09 PM
  5. Replies: 8
    Last Post: Oct 25th, 2004, 10:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •