Question: I have a search button that goes into my database and searches on a name but when there are two entries for the same name it is only bringing me back one and i can't figure out why. Below is the code I am using now.

```
 public boolean search(String str1, String username){//search function for the access history search with two parameters
    boolean condition = true;
    String dataSourceName = "securitySystem";//setting string datasource to the securitySystem datasource
    String dbUrl = "jdbc:odbc:" + dataSourceName;//creating the database path for the connection
    try{
        //Type of connection driver used    
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        //Connection variable or object param: dbPath, userName, password
        Connection con = DriverManager.getConnection(dbUrl, "", "");

        Statement statement = con.createStatement();//creating the statement which is equal to the connection statement

        if (!(username.equals(""))){
            PreparedStatement ps = con.prepareStatement("select * from securitysystem.accessHistory where name = ?");//query to be executed
            ps.setString(1, username);//insert the strings into the statement
            ResultSet rs=ps.executeQuery();//execute the query
            if(rs.next()){//while the rs (ResultSet) has returned data to cycle through
                JTable table = new JTable(buildTableModel(rs));//build a JTable which is reflective of the ResultSet (rs)
            JOptionPane.showMessageDialog(null, new JScrollPane(table));//put scrollpane on the table
        }
        else{
                JOptionPane.showMessageDialog(null,"There has been no system logins at this time");// else- show a dialog box with a message for the user
            }
}
statement.close();//close the connection
    } catch (Exception e) {//catch error
        System.out.println(e);
    }
    return condition;   
}

```

Answer: 
Matthias is spot on.

A @Stateless annotated bean is an EJB which by default provides [Container-Managed-Transactions](http://docs.oracle.com/javaee/6/tutorial/doc/bncij.html). CMT will by default create a new transaction if the client of the EJB did not provide one.

> 
> **Required Attribute** If the client is running within a transaction and
>  invokes the enterprise bean’s method, the method executes within the
>  client’s transaction. If the client is not associated with a
>  transaction, the container starts a new transaction before running the
>  method.
> 
> 
> The Required attribute is the implicit transaction attribute for all
>  enterprise bean methods running with container-managed transaction
>  demarcation. You typically do not set the Required attribute unless
>  you need to override another transaction attribute. Because
>  transaction attributes are declarative, you can easily change them
>  later.
> 
> 
> 

In the recent java-ee-7 [tuturial](http://www.oracle.com/technetwork/articles/java/jaxrs20-1929352.html) on jax-rs, Oracle has example of using EJBs (@Stateless).

> 
> ... the combination of EJB's @javax.ejb.Asynchronous annotation and
>  the @Suspended AsyncResponse enables asynchronous execution of
>  business logic with eventual notification of the interested client.
>  Any JAX-RS root resource can be annotated with @Stateless or
>  @Singleton annotations and can, in effect, function as an EJB ..
> 
> 
> 

Main difference between @RequestScoped vs @Stateless in this scenario will be that the container can pool the EJBs and avoid some expensive construct/destroy operations that might be needed for beans that would otherwise be constructed on every request.
When you don't want to make your root resource as an EJB (by annotating it with `@Stateless`), you can use a `UserTransaction`.

```
@Path("/things")
@RequestScoped
public class ThingsResource{

    @POST
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response create(final Thing thing){
        utx.begin();
        em.joinTransaction();
        final ThingEntity thingEntity = new ThingEntity(thing);
        em.persist(thing);
        utx.commit();
        final URI uri = uriInfo.getAbsolutePathBuilder()
            .path(Long.toString(thingEntity.getId())).build();
        return Response.created(uri).build();
    }

    @PersistenceContext(unitName = "somePU")
    private transient EntityManager em;

    @Resource
    private transient UserTransaction ut;

    @Context
    private transient UriInfo uriInfo;
}

```