anand_2000v
asked on
Commit throws exception in jboss when autocommit is set to true
I have encountered the error java.sql.SQLException: You cannot commit with autocommit set! while commiting the transaction. I am taking the connection via javax.sql.DataSource & I didn't set autocommit to false while taking connection the default is = true in this case as i've not done any entry regarding autocommit in my oracle-ds.xml file
My oracle-ds.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi- name>
<connection-url>jdbc:oracl e:thin:@lx ora.dlh.st .com:1521: adcsdev</c onnection- url>
<driver-class>oracle.jdbc. driver.Ora cleDriver< /driver-cl ass>
<user-name>adcsprd</user-n ame>
<password>adcsprd</passwor d>
<exception-sorter-class-na me>org.jbo ss.resourc e.adapter. jdbc.vendo r.OracleEx ceptionSor ter</excep tion-sorte r-class-na me>
<valid-connection-checker- class-name >org.jboss .resource. adapter.jd bc.vendor. OracleVali dConnectio nChecker</ valid-conn ection-che cker-class -name>
<new-connection-sql>select 1 from dual</new-connection-sql>
<check-valid-connection-sq l>select 1 from dual</check-valid-connecti on-sql>
<min-pool-size>2</min-pool -size>
<max-pool-size>20</max-poo l-size>
<idle-timeout-minutes>1</i dle-timeou t-minutes>
<metadata>
<type-mapping>Oracle8i</ty pe-mapping >
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>OracleDS-QA</jn di-name>
<connection-url>jdbc:oracl e:thin:@eu x047.sgp.s t.com:1551 :adcsqa</c onnection- url>
<driver-class>oracle.jdbc. driver.Ora cleDriver< /driver-cl ass>
<user-name>adcsprd</user-n ame>
<password>jdhOHijB</passwo rd>
<exception-sorter-class-na me>org.jbo ss.resourc e.adapter. jdbc.vendo r.OracleEx ceptionSor ter</excep tion-sorte r-class-na me>
<valid-connection-checker- class-name >org.jboss .resource. adapter.jd bc.vendor. OracleVali dConnectio nChecker</ valid-conn ection-che cker-class -name>
<new-connection-sql>select 1 from dual</new-connection-sql>
<check-valid-connection-sq l>select 1 from dual</check-valid-connecti on-sql>
<min-pool-size>2</min-pool -size>
<max-pool-size>20</max-poo l-size>
<idle-timeout-minutes>1</i dle-timeou t-minutes>
<metadata>
<type-mapping>Oracle8i</ty pe-mapping >
</metadata>
</local-tx-datasource>
</datasources>
By default the autocommit is true so why the error is coming after commiting the transaction. I've also made a test program with simple implementation as:
public class AutoCommitTest
{
public static void main(String[] args)
{
Connection conn=null;
Statement stmt=null;
try
{
Driver driver = (Driver) Class.forName("oracle.jdbc .driver.Or acleDriver ").newInst ance();
DriverManager.registerDriv er(driver) ;
conn = DriverManager.getConnectio n("jdbc:or acle:thin: @lxora.dlh .st.com:15 21:adcsdev ","adcsprd ","adcsprd ");
System.out.println("Conn== =" + conn.getAutoCommit());
conn.setAutoCommit(true);
stmt=conn.createStatement( );
int x=stmt.executeUpdate("upda te am_temp set name='aa'");
System.out.println("x=" + x);
conn.commit();
System.out.println("Table updated");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{if(conn!=null) conn.close();} catch(Exception e) {}
}
}
}
in this case i received no error as " java.sql.SQLException: You cannot commit with autocommit set! "
Does jboss use any special internal handling mor managing the transaction. How can I come out from the
problem? Also the method _con.getMetaData().getJDBC MajorVersi on() returns unsupported feature exception as i am using ojdbc14.jar. Is it better to user classes11.jar or ojdbc14.jar ??
Product Name : Document Management System
S/w:
-----
Java 1.5 update 11
jboss ver 4.0.3
Oracle 8i
My oracle-ds.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>OracleDS</jndi-
<connection-url>jdbc:oracl
<driver-class>oracle.jdbc.
<user-name>adcsprd</user-n
<password>adcsprd</passwor
<exception-sorter-class-na
<valid-connection-checker-
<new-connection-sql>select
<check-valid-connection-sq
<min-pool-size>2</min-pool
<max-pool-size>20</max-poo
<idle-timeout-minutes>1</i
<metadata>
<type-mapping>Oracle8i</ty
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>OracleDS-QA</jn
<connection-url>jdbc:oracl
<driver-class>oracle.jdbc.
<user-name>adcsprd</user-n
<password>jdhOHijB</passwo
<exception-sorter-class-na
<valid-connection-checker-
<new-connection-sql>select
<check-valid-connection-sq
<min-pool-size>2</min-pool
<max-pool-size>20</max-poo
<idle-timeout-minutes>1</i
<metadata>
<type-mapping>Oracle8i</ty
</metadata>
</local-tx-datasource>
</datasources>
By default the autocommit is true so why the error is coming after commiting the transaction. I've also made a test program with simple implementation as:
public class AutoCommitTest
{
public static void main(String[] args)
{
Connection conn=null;
Statement stmt=null;
try
{
Driver driver = (Driver) Class.forName("oracle.jdbc
DriverManager.registerDriv
conn = DriverManager.getConnectio
System.out.println("Conn==
conn.setAutoCommit(true);
stmt=conn.createStatement(
int x=stmt.executeUpdate("upda
System.out.println("x=" + x);
conn.commit();
System.out.println("Table updated");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try{if(conn!=null) conn.close();} catch(Exception e) {}
}
}
}
in this case i received no error as " java.sql.SQLException: You cannot commit with autocommit set! "
Does jboss use any special internal handling mor managing the transaction. How can I come out from the
problem? Also the method _con.getMetaData().getJDBC
Product Name : Document Management System
S/w:
-----
Java 1.5 update 11
jboss ver 4.0.3
Oracle 8i
What code is it trying to run when it falls over?
Sorry, I forgot something, you should read:
So you should:
- EITHER turn off autocommit
- OR remove the commit() & rollback() calls from your code
So you should:
- EITHER turn off autocommit
- OR remove the commit() & rollback() calls from your code
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I want to know that why the second program [AutoCommitTest ] works without throwing the exception and 1st one is not working is there any issue with jboss connection management ?
Ideally, the second program also should throw an exception, unless :
1. The driver being loaded does not throw an exception for commit when autocommi=true
2. For whatever reason, the autocommit is not being set to true in the second program
1. The driver being loaded does not throw an exception for commit when autocommi=true
2. For whatever reason, the autocommit is not being set to true in the second program
ASKER
But I am using the same driver ojdbc14.jar in both the programs. How come it is possible that second program will throw exception ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To set autocommit to false, try this :
<local-tx-datasource>
<blah blah blah>
<connection-property name="autoCommit">false</c onnection- property>
<blah blah blah>
</local-tx-datasource>
<local-tx-datasource>
<blah blah blah>
<connection-property name="autoCommit">false</c
<blah blah blah>
</local-tx-datasource>
ASKER
Here is my scenario
MY WEBAPP---------> DB
MY WEBAPP--------->3rd party tool------>DB
there are 2 different transactions on same DB one is directly interacting with db other is interacting with db by 3rd party tool now if I set the autocommit to false in both the transactions and I perform some manipulation in the common table then my table got locked I got stuck. So i think it is better to check by taking the status of autocommit in a boolean variable, and call commit/rollback conditionally or is there other way pls suggest ?
Also Is there any diff between when i set autocommit to false in my code itself or by doing <connection-property name="autoCommit">false</c onnection- property> ?
MY WEBAPP---------> DB
MY WEBAPP--------->3rd party tool------>DB
there are 2 different transactions on same DB one is directly interacting with db other is interacting with db by 3rd party tool now if I set the autocommit to false in both the transactions and I perform some manipulation in the common table then my table got locked I got stuck. So i think it is better to check by taking the status of autocommit in a boolean variable, and call commit/rollback conditionally or is there other way pls suggest ?
Also Is there any diff between when i set autocommit to false in my code itself or by doing <connection-property name="autoCommit">false</c
the <connection-property name="autoCommit">false</c onnection- property> tells JBoss that autocommit is false. So tehn it wont throw the exception
On the contarary, if you change it through code, JBoss wont know about it - its just a wrapper on top that does the check - so it will fail.
also, as I suggested earlier, use teh boolean for autocommit - it doesnt fail :)
On the contarary, if you change it through code, JBoss wont know about it - its just a wrapper on top that does the check - so it will fail.
also, as I suggested earlier, use teh boolean for autocommit - it doesnt fail :)
So you should:
- turn off autocommit
- remove the commit() & rollback() calls from your code
There are no other ways to avoid that error.