Good morning. I have a multithreaded application that accesses in reading writing to a DB mySql . Use myBatis . For session management it wrote the following class:
public class ConnectionMySQL {
private static final Logger log = Logger.getLogger(ConnectionMySQL.class);
private static SqlSessionFactory sqlSessionFactory;
private ConnectionMySQL() {
}
static {
try {
String resource = "com/application/dao/config/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (Throwable e) {
log.error("Impossibile avviare la connessione sul DB", e);
}
}
public static SqlSessionFactory getSession() {
return sqlSessionFactory;
}
}
Whenever I run a query I run these operations :
public void start() {
System.out.println(new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime())+" ***** Start ****");
long start = System.currentTimeMillis();
SqlSession sessione = ConnectionMySQL.getSession().openSession();
try {
..........
..........
}catch (MessagingException e) {
log.warn("Impossibile inviare messaggio di notifica");
}finally {
sessione.close();
long end = System.currentTimeMillis();
log.info("Numero thread :"+numeroThread+" - Tempo di esecuzione "+ (end - start) +" ms");
}
}
I am not very experienced in MySQL and myBatis . I wanted to know if this is the proper way to handle the multi thread with myBatis. Thank's.