<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Concurrence management
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="http://community.jboss.org/people/fox82">Alessandro Laurucci</a> in <i>EJB3</i> - <a href="http://community.jboss.org/message/612126#612126">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hi to all,</p><p>I'm developing my first EJB3 application with eclipse. I don't know i manage the insert into a database in case of duplicate key.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I use this code:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><span style="text-decoration: underline;"><strong>Table KGTWANAUSER01:</strong></span></p><p>-user (key)</p><p>-password</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong style="text-decoration: underline;">Entity bean:</strong></p><p>import java.io.Serializable; </p><p>import javax.persistence.Column; </p><p>import javax.persistence.Entity; </p><p>import javax.persistence.Id; </p><p>import javax.persistence.Table; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Entity @Table(name="KGTWANAUSER01") </p><p>public class User implements Serializable { </p><p>private static final long serialVersionUID = 1L; </p><p>@Id @Column(name="USER") private String USER; </p><p>@Column(name="PASSWORD") private String PASSWORD; </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public User(String us, String pswd) </p><p>{ this.user=us; this.password=pswd; } </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public String getUSER() </p><p>{ return USER; } </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public String getPASSWORD() </p><p>{ return PASSWORD; } </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public void setUSER(String uSER) </p><p>{ USER = uSER; } </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public void setPASSWORD(String pASSWORD) </p><p>{ PASSWORD = pASSWORD; } </p><p>}</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><a class="jive-link-anchor-small">#</a></p><p><a class="jive-link-anchor-small"><p class="programlistingco"><span style="text-decoration: underline;"><strong>Stateless Session Bean (register method):</strong></span></p><p class="programlistingco">....</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>public String register(String user, String password)</p><p>&#160;&#160; {&#160; </p><p>&#160;&#160;&#160;&#160;&#160; User us = new User (user, password)&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160; List existing = em.createQuery(</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; "select username from User where username = #{user.username}")</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .getResultList();</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </p><p>&#160;&#160;&#160;&#160;&#160; if (existing.size()==0)</p><p>&#160;&#160;&#160;&#160;&#160; {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; em.persist(us);</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return "OK";</p></a></p><p><a class="jive-link-anchor-small">#</a></p><p><a class="jive-link-anchor-small"><p style="display: inline !important;">&#160;&#160;&#160;&#160;&#160; }&#160;&#160;&#160;&#160;&#160;&#160; </p></a></p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&#160;&#160;&#160;&#160;&#160; else</p><p>&#160;&#160;&#160;&#160;&#160; {</p><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return "NO;"</p><p>&#160;&#160;&#160;&#160;&#160; }</p><p>&#160;&#160; }</p><p>....</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco">My problem is this:</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco">I have two client A e B that want to create their new user, both with the username "ale":</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco">A call register method, execute the select, no other user "ale" are register so existing.size=0;</p><p class="programlistingco">B call the same method, and also in this case no other user "ale" are register so existing.size=0;</p><p class="programlistingco">A make em.persist(us) adn the transaction is OK</p><p class="programlistingco">B try to make the insert with the persist, but the user now exist, so the application crashs and Jboss show me a list of errors caused by the duplicate key...</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco">Ho i can solve this problem?</p><p class="programlistingco">Thanks to All</p><p class="programlistingco" style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p class="programlistingco">Alessandro</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="http://community.jboss.org/message/612126#612126">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>