Skip to main content

MYSQL DataSource with Glassfish under Netbeans 6.9

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
6 replies [Last post]
Miloud_j2ee
Offline
Joined: 2011-04-06

HI,
Can anyone tell me how to set up a MySQL Datasource with Glassfish under Netbeans 6.9
so that I can use the following code:
1)EntityManagerFactory emf= Persistence.createEntityManagerFactory("ejbPU");
2)Authentification em = (Authentification) emf.createEntityManager();

where authentification is an entity class obtained from a database
so far I can get "emf" with success but failed to get "em" and get the error: cannot acquire datasource [MYSQL/Datasource]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
arungupta
Offline
Joined: 2003-06-13

Hi Miloud_j2ee,

The following blog will help you answer the question:

http://blogs.sun.com/arungupta/entry/totd_122_creating_a_jpa

Let me know if you are looking for something else.

Thanks,
-Arun

On 4/6/11 1:49 PM, forums@java.net wrote:
> HI,
>

Miloud_j2ee
Offline
Joined: 2011-04-06

HI Arungputa,
I have done those steps before but the problem is that glassfish cannot locate the jndi of the datasource [MYSQL/Datasource] although it is generated after the the completion of the wizard of creating a an entity class from a database, and it is located in sun-resources.xml.
Best regards

arungupta
Offline
Joined: 2003-06-13

Can you provide the exact steps on how you are creating the data source ?

FYI, a comprehensive screencast showing Java EE 6 tooling with NetBeans
and GlassFish is available at:

http://blogs.sun.com/arungupta/entry/screencast_37_java_ee_6

-Arun

On 4/11/11 1:51 AM, forums@java.net wrote:
> HI Arungputa,
> *I have done those steps before but the problem is that glasfish can locate
> the jndi of the datasource [MYSQL/Datasource] although it is generated
> after the the completion of the wizard of creating a an entity class from a
> database, and it is located in sun-resources.xml.*
> Best regards
>
>
>
> --
>
> [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

Miloud_j2ee
Offline
Joined: 2011-04-06

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

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.

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
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

arungupta
Offline
Joined: 2003-06-13

Miloud_j2ee,

See in line response ...

On 4/15/11 5:58 AM, forums@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:

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

Miloud_j2ee
Offline
Joined: 2011-04-06

Hi Arun,
1)Where are you accessing this EntityManager ?
I am Accessing this in LoginForm.java which extends JDialog. by the way I cannot use dependency Injection like this :
@PersistenceUnit EntityManagerFactory emf;
Although I am using JDK 6
2)with Mysql I have password and I can connect tothe datasource :MYSQL/datasource and the propertoes are correctly generated in sun-resources.xl which is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_mysql_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="mysql"/>
<property name="User" value="root"/>
<property name="Password" value="miloud"/>
<property name="URL" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="mysql/datasource" object-type="user" pool-name="mysql_mysql_rootPool"/>
</resources>

Many thanks