users@glassfish.java.net

Re: MYSQL DataSource with Glassfish under Netbeans 6.9

From: Arun Gupta <arun.p.gupta_at_oracle.com>
Date: Sun, 17 Apr 2011 10:40:21 +0200

Miloud_j2ee,

See in line response ...

On 4/15/11 5:58 AM, forums_at_java.net wrote:
> the steps for creating the datasource and using it in the loginform in
> order
> to take the *username/password *and match it(using the authentification
> entity)with the entered one in this form
> *
> 1)After setting up the JDBC with Mysql and created a table authentification
> where I put the password and user names
Make sure the JDBC resource has a login/password. The default schema for
MySQL is root with no password and JDBC resource cannot be accessed with
no password credentials.

> 2)I choose create entity from database wizard which ask for jndi, I put
> Mysql/datasource and used the url configured
> "jdbc:mysql://localhost:3306/mysql [root on Default schema]"which is listed
> in drop down box, I also checked "Create persistance Unit" and I have
> choosen authentification table and moved in the text area name selected
> tables.
This sounds good.

> 3)This step generates entity class called authentification
> 4) For the mapping step of the wizard I have chosen the default
> *In the code of loginform Class then I have imported the required
> persistence package in this class and used the follwing code snippet
> *1)EntityManagerFactory emf=
> Persistence.createEntityManagerFactory("ejbPU"); // succeeded without
> error and I emf is not empty
Where are you accessing this EntityManager ?

The preferred way for an EJB is:

@PersistenceContext EntityManager em;

For a Servlet, you can do:

@PersistenceUnit EntityManagerFactory emf;

emf.createEntityManager();

I tried this using NetBeans 7.0 RC1 and GlassFish 3.1 and worked fine. I
see the following entries in domains/domain1/config/domain.xml:

<jdbc-connection-pool connection-validation-method="auto-commit"
datasource-classname="com.mysql
.jdbc.jdbc2.optional.MysqlDataSource" wrap-jdbc-objects="false"
res-type="javax.sql.DataSource" name
="mysql_states_glassfishPool">
       <property name="URL"
value="jdbc:mysql://localhost:3306/states"></property>
       <property name="driverClass"
value="com.mysql.jdbc.Driver"></property>
       <property name="Password" value="glassfish"></property>
       <property name="portNumber" value="3306"></property>
       <property name="databaseName" value="states"></property>
       <property name="User" value="glassfish"></property>
       <property name="serverName" value="localhost"></property>
     </jdbc-connection-pool>
     <jdbc-resource pool-name="mysql_states_glassfishPool"
jndi-name="mysql/dataresource"></jdbc-resource>

Of course, the table name would differ in your case.

-Arun

> 2)Authentification em = (Authentification)
> emf.createEntityManager();* // Here I get
> an error :* Cannot acquire [Mysql/datasource]*
>
> After this I intend to use *"em" *and select the *username/passwordand
> do a**
> select** from authentification
> *
>
>
> --
>
> [Message sent by forum member 'Miloud_j2ee']
>
> View Post: http://forums.java.net/node/789364
>
>

-- 
Blog: http://blogs.sun.com/arungupta
Twitter: http://twitter.com/arungupta