JDBC INSERT not working

Hi,

I have an issue with JDBC insert query on camunda ACT_HI_COMMENT connected as Mysql. I have written an execution listener to be called after task complete. In which i written statement to insert data but unfortunately i was not able to insert , it doesnt shows any exception.

I have tried adding a new table in camunda DB and insert data in the same way, it was inserted successfully.

Hoping some help regarding this

Thanks
Manmohan

Hoping some response

Hi @manmohanm,

Could you please elaborate how you tried to insert comment to the table ACT_HI_COMMENT? Did you use TaskService#createCommenct()? Could you please share the code of the corresponding listener?

Cheers,
Roman

Hi @roman.smirnov. Thank you for your response . Requesting you to go through the below mentioned code. Actually I am unable to find the exact issue .

public class UserComments implements TaskListener {

    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    PreparedStatement preparedStatement = null;

    @Override
    public void notify(DelegateTask task) {

        Context envCtx;
        DataSource ds =null;
        Connection conn =null;
        Statement statement=null;
        PreparedStatement preparedStmt=null;
        try {    
            Context initCtx = new InitialContext();
            envCtx = (Context) initCtx.lookup("java:comp/env");
            ds= (DataSource)
                    envCtx.lookup("jdbc/ProcessEngine");

            conn = ds.getConnection();    
            conn.setAutoCommit(false); 
            String comm="comment";
            String messages="test";    
            System.out.println(task.getId().toString());
            System.out.println(task.getName().toString());
            System.out.println(task.getProcessInstanceId().toString());    
            String mysqlString = "INSERT INTO ACT_HI_COMMENT(ID_,TYPE_,MESSAGE_) VALUES (?,?,?)";
            preparedStmt = conn.prepareStatement(mysqlString);
            preparedStmt.setString(1,"10");
            preparedStmt.setString(2, comm);
            preparedStmt.setString(3, messages);
            preparedStmt.executeUpdate();

        } catch (NamingException e) {
        
            e.printStackTrace();
        } catch (Exception e) {
            

            e.printStackTrace();
        }



        finally{ 
            try {
                conn.close();
            } catch (SQLException e) {
                
                e.printStackTrace();
            }


            
        }



    }

}

Hi @manmohanm,

Instead of your approach you should use the existing public API to create a comment. This could look like as follows:

public void notify(DelegateTask delegateTask) {
  ProcessEngineServices processEngineServices = delegateTask.getProcessEngineServices();
  TaskService taskService = processEngineServices.getTaskService();
  taskService.createComment(delegateTask.getId(), delegateTask.getProcessInstanceId(), "your message...");
}

Cheers,
Roman

Hi @roman.smirnov ,

Thank you very much :slight_smile: . Implemented your code and its working now.

Regards

Manmohan