Java, JPA, Glassfish, Invalid resource : jdbc/__default__pm

后端 未结 8 866
不知归路
不知归路 2020-12-02 17:32

I use Glassfish 3.1.2.2 (build 5), JPA, EclipseLink, MySQL

I created MySQL pool via Glassfish admin panel. Ping to MySQL from GF admin panel is ok.

I created

相关标签:
8条回答
  • 2020-12-02 17:39

    This could happen due to a NetBeans bug they say they had fixed but seemingly they had not. As a solution, I ended up removing glassfish-resources.xml altogether (as I couldn't make persistence.xml pick it up with any kind of the workarounds) and using @DataSourceDefinition annotation instead.

    My configuration uses a separate DataSourceBean.java file for @DataSourceDefinition:

    @Singleton
    @Startup
    @DataSourceDefinition(name="java:global/jdbc/myDataSource",
        className="com.microsoft.sqlserver.jdbc.SQLServerDataSource",
        url="jdbc:sqlserver://127.0.0.1:1433;databaseName=myDB",
        user="myuser",
        password="mypassword"
    )
    public class DataSourceBean {
    }
    

    persistence.xml looks like that:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                 xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
                 version="2.1">
        <persistence-unit name="myUnit" transaction-type="JTA">
            <jta-data-source>java:global/jdbc/myDataSource</jta-data-source>
            <properties>
                <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            </properties>
        </persistence-unit>
    </persistence>
    
    0 讨论(0)
  • 2020-12-02 17:40

    (The same post of mine but with proper account now):

    When configuring persistence with your setup, you only set the JNDI name for the JDBC pool in persistence.xml. Optional, you may set the target database name.

    <persistence-unit name="foo" transaction-type="JTA">
        <jta-data-source>jdbc/mysql</jta-data-source>
        <!--optional-->
        <property name="eclipselink.target-database" value="MySQL4"/>
    </persistence-unit>
    

    I also encourage to change the 'drop-and-create-table' to 'create-tables', so that you don't loose data, and this should be be providing EclipseLink's properties in following way:

    <property name="eclipselink.ddl-generation" value="create-tables"/>
    

    and also handy

    <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    

    that will create schema and sql scripts.

    For more information visit: http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL or http://docs.oracle.com/cd/E19798-01/821-1752/gbwmj/index.html

    0 讨论(0)
  • 2020-12-02 17:44

    I had this problem.I set jdbc connection pool and jdbc resource in admin consol. But i did not set data source in persistence.xml .

      <persistence-unit name="OnlinePrintService-warPU" transaction-type="JTA">
        <jta-data-source>jdbc/your-name</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
          <property name="javax.persistence.schema-generation.database.action" value="create"/>
        </properties>
      </persistence-unit>
    
    0 讨论(0)
  • 2020-12-02 17:46

    I had the same problem.

    The solution (for anybody that still have this issue):

    1. if you are using the NetBeans IDE 8.1 with Glassfish 4.1.1, I advise you to change it to Glassfish 4.1

    2. Go to the left panel in NetBeans.

    Click on services > server > glassfish then right-click (in Glassfish server) and choose view domain admin console, a web page should show up.

    Go to the left and choose resources > JDBC and JDBC connection pool.

    Add a new connection pool by clicking on new, type the name of your pool.

    Next, choose the javax.sql.ConnectionPoolDataSource and the datadriver (in my case is MySQL) and click next.

    After that, you should enter all the needed information for your database.

    1. Go back to Resources > JDBC. This time, JDBC Resources create a new JDBC resources (for me, I named it jdbc/test). Don't forget to link it with the connection pool you already created.

    2. In NetBeans go to your ejb project and modify the persistence.xml file. Change the datasource to the database resource (in my case jdbc/test) and save all.

    That should work.

    0 讨论(0)
  • 2020-12-02 17:46

    I had the same problem when I run/deploy an app on a server created by netbeans instalattion wizard.

    To solve I downloaded the last glassfish version from official site and went to: Netbeans > "Service's Tab" > "Servers" > "Add Server...".

    Remember to change your project configurations to use the new glassfish server instance.

    0 讨论(0)
  • 2020-12-02 17:53

    If you have only created a MySQL connection pool, you must also create a JDBC resource. This can be created from the context menu above the one you used to create the connection pool.

    Example Glassfish jdbc resource setup

    In my Glassfish, my JDBC resource, jdbc/__default is using the connection pool mysql_lemon.

    0 讨论(0)
提交回复
热议问题