0 Replies Latest reply on Nov 15, 2010 6:37 AM by wilsoneto

    SLSB transaction Commit

    wilsoneto

      Hello, I am new to Seam, but really putting efforts to learn it. But for the past 2 days a have been researching and trying to make my application read from serial port input and save in the database, I have a running thread reading the serial input, but I could not get the transaction to commit the the database. There are some concepts still fuzzy in my mind if someone could give-me a hint what to do. Here is the code:



      @Stateless
      @Name("serialThread")
      @Scope(ScopeType.APPLICATION)
      @Startup
      @TransactionManagement(TransactionManagementType.CONTAINER)
      public class TwoWaySerialComm implements TwoWaySerialComm_I, Runnable
      {
           @In(create=true)
           public EstatisticaHome estatisticaHome;
           
          @PersistenceContext
           public EntityManager em;
            
          InputStream in;
           StringBuffer sb = new StringBuffer();
      
           @Observer("org.jboss.seam.postInitialization")
          public void create(){
               try {
                     connect("COM1");
                } catch (Exception e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                     System.out.println("Erro = "+e.getMessage());
                }
          }
         
          public void connect ( String portName ) throws Exception{
               System.out.println("READING SERIAL INPUT");
                 CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
              if ( portIdentifier.isCurrentlyOwned() )
              {
                  System.out.println("Error: Port is currently in use");
              }
              else
              {
                  CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
                  
                  if ( commPort instanceof SerialPort )
                  {
                      SerialPort serialPort = (SerialPort) commPort;
                      serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                      
                      in = serialPort.getInputStream();
                      this.run();
                  }
                  else
                  {
                      System.out.println("Error: Only serial ports are handled by this example.");
                  }
              }     
          }
          @TransactionAttribute
          public void persistir(EstatisticaHome e){
               em.persist(e.getInstance());
          }
      
               
          public void run ()
          {
                   
                  byte[] buffer = new byte[1024];
                  int len = -1;
                  try
                  {
                      while ( ( len = this.in.read(buffer)) > -1 )
                      {
                          System.out.println("Input = "+new String(buffer,0,len));
                          sb.append(new String(buffer,0,len));
                          String palavra = sb.toString();
                          System.out.println("Palavra = "+palavra);
                          int indice = palavra.indexOf("*");
                          if(indice == -1){
                               continue;
                          }
                          else{
                               sb = new StringBuffer();
                            
                                  Calendar cal = Calendar.getInstance();
                                  long time = cal.getTimeInMillis();
      
                              
                               int indiceMestre = palavra.indexOf(":");
                                int indiceFim = palavra.indexOf("*");
                                
                                String vagas = new String();
                                int idMestre = new Integer(palavra.substring(0, indiceMestre)).intValue();
                                System.out.println("ID Mestre = "+idMestre);
                                
                                vagas = palavra.substring(indiceMestre+1, indiceFim);
                                vagas = vagas.toUpperCase();
                                System.out.println("Vagas = "+vagas);
                                System.out.println(" ");
                                
                                char[] vagasChar = vagas.toCharArray();
                                
                                for(int i=0; i<vagasChar.length; i++){
                                     Estatistica e = new Estatistica();
                                     e.setIdSensor(i);
                                     e.setStatus(vagasChar[i]);
                                     e.setTime(time);
                                     estatisticaHome.setInstance(e);
                                           persistir(estatisticaHome);
                                     
                                }
                                
                          }
                          
                      }
                  }
                  catch ( IOException e )
                  {
                      e.printStackTrace();
                  }            
              }
          
      
      }
      
      



      Once a get an array of char from serial input, I need to save in the database each value in the char in a separate registry.


      When I start the application ina JBoss Server I gat this output after information comes in the serial port:



      08:33:57,841 INFO  [STDOUT] CharArray = OOLL
      08:33:57,841 INFO  [STDOUT]  
      08:33:57,957 INFO  [STDOUT] Hibernate: 
          insert 
          into
              estacionamento.estatistica
              (id_sensor, status, time) 
          values
              (?, ?, ?)
      08:33:58,046 INFO  [STDOUT] Hibernate: 
          insert 
          into
              estacionamento.estatistica
              (id_sensor, status, time) 
          values
              (?, ?, ?)
      08:33:58,051 INFO  [STDOUT] Hibernate: 
          insert 
          into
              estacionamento.estatistica
              (id_sensor, status, time) 
          values
              (?, ?, ?)
      08:33:58,057 INFO  [STDOUT] Hibernate: 
          insert 
          into
              estacionamento.estatistica
              (id_sensor, status, time) 
          values
              (?, ?, ?)
      



      My database stays clear unfortunatelly the transaction does not commit and after a while it timesout. I have tryed a lot of solutions I have found in my research, but it looks like I could not gat a complete solution and there are a few. I fell like i´m mixing parts of several solutions but I wont work like that. If you more experient guys could help!