Reputation: 53
I am testing out the atomikos transaction and database connectivity stuff, as a part of it i tried to execute the below code,
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import javax.sql.XAConnection;
import javax.transaction.Transaction;
import javax.transaction.xa.XAResource;
import com.atomikos.datasource.xa.jdbc.JdbcTransactionalResource;
import com.atomikos.icatch.config.UserTransactionServiceImp;
import com.atomikos.icatch.jta.UserTransactionManager;
import com.atomikos.persistence.imp.StateRecoveryManagerImp;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
/**
* Working out how to use Atomikos, before building {@link Main}. It is not
* intended that you write your applications like this - use JCA+EJB or Spring
* instead! There is way too much boilerplate code here. Based on examples found
* at the Atomikos website.
*/
public class TestAtomikos {
public static void main(String[] args) throws Exception {
MysqlXADataSource mysql = new MysqlXADataSource();
mysql.setUser("root");
mysql.setPassword("root");
mysql.setUrl("jdbc:mysql://localhost:3306/world?useSSL=false");
JdbcTransactionalResource mysqlResource = new JdbcTransactionalResource(
"jdbc/mysql", mysql);
UserTransactionServiceImp utsi = new UserTransactionServiceImp();
utsi.registerResource(mysqlResource);
Properties prop = new Properties();
InputStream input = null;
//StateRecoveryManagerImp srmi = new StateRecoveryManagerImp(null);
try {
input = new FileInputStream("C:\\Users\\abcd\\eclipse\\workspace_Tomcat\\JNDI\\src\\main\\resources\\jta.properties");
// load a properties file
prop.load(input);
}
catch (IOException ex) {
ex.printStackTrace();
}
utsi.init(prop);
//utsi.init();
UserTransactionManager utm = new UserTransactionManager();
//utm.init();
utm.begin();
Transaction tx = utm.getTransaction();
XAConnection xamysql = mysql.getXAConnection();
XAResource db = xamysql.getXAResource();
tx.enlistResource(db);
Connection connection = xamysql.getConnection();
PreparedStatement stmt=connection.prepareStatement("SELECT ID, Name FROM city");
ResultSet rs = null;
rs = stmt.executeQuery("SELECT ID, Name FROM city");
while(rs.next())
{
System.out.println(rs.getInt("ID") + " " + rs.getString("Name"));
}
}
}
But i am getting an error,
log4j:WARN No appenders could be found for logger (com.atomikos.logging.LoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NoSuchMethodError: com.atomikos.persistence.imp.StateRecoveryManagerImp: method <init>()V not found
at com.atomikos.icatch.standalone.UserTransactionServiceImp.createDefault(UserTransactionServiceImp.java:205)
at com.atomikos.icatch.standalone.UserTransactionServiceImp.init(UserTransactionServiceImp.java:258)
at com.atomikos.icatch.config.UserTransactionServiceImp.init(UserTransactionServiceImp.java:405)
at com.atomikos.icatch.config.UserTransactionServiceImp.init(UserTransactionServiceImp.java:577)
at com.test.abcd.TestAtomikos.main(TestAtomikos.java:57)
I tried executing the same by integrating atomikos with tomcat and invoking a servlet which makes a call to the db. I got the same error, i tired checking the code of the StateRecoveryManagerImp
online, i see the init method defined. I am not sure what is causing this issue. I tried just the database stuff, it worked fine, i was able to execute the query and get the results as well.
I tried using various versions of atomikos jars, but no luck. Any suggestions on how to get this fixed?
Upvotes: 1
Views: 1609
Reputation: 44965
The exception that you get which is:
NoSuchMethodError: com.atomikos.persistence.imp.StateRecoveryManagerImp: method ()V not found
means that it tries at runtime to access to the default constructor (constructor with no arguments) of the class StateRecoveryManagerImp
and it cannot find it.
This is a typically an exception that you face when you build your application with one version of your library and you run it against a different version that is not compatible.
Here as far as I can see from the source code, I believe that you compiled your code with transactions
3.9.0
or higher that has no constructor defined which means that we have the default constructor (as you can see here), and your application uses at Runtime an older version which has a constructor of type public StateRecoveryManagerImp ( ObjectLog objectlog )
which means that we have no default constructor (as you can see here) so when it tries to call it at runtime it fails.
To fix your issue simply check your classpath used at Runtime and make sure that you have only the version of transactions
corresponding to the one used at compile time.
Upvotes: 1