jpa persist()函数在TABLE_PER_TENANT(multi_schema)多租户策略中给出错误

时间:2017-03-03 13:04:37

标签: java mysql jpa multi-tenant

我正在使用"共享数据库/单独模式"开发多租户网络应用程序。使用java,jpa(eclipselink),mysql的方法。我的持久性文件如下所示:

    <persistence-unit name="GroupBuilderPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
            <exclude-unlisted-classes>false</exclude-unlisted-classes>
            <properties>
                <property name="eclipselink.cache.shared.default" value="false"/>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?"/>
                <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
<--- Here goes other properties definition -->
        </persistence-unit>

现在这是我的EntityMangerFactory和EntityManager:

emfForTenant = Persistence.createEntityManagerFactory("GroupBuilderPU");
EntityManager em = emfForTenant.createEntityManager();
        em.setProperty("eclipselink.tenant-id", schemaNameAsTenantId);

我有一个实体MaterialUnit:

@Entity
@Multitenant(MultitenantType.TABLE_PER_TENANT)
@TenantTableDiscriminator(type = TenantTableDiscriminatorType.SCHEMA, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
public class MaterialUnit implements Serializable {
    //private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Size(min = 1, max = 128)
    @Column(unique=true)
    private String unitName;

现在操作出错:

MaterialUnit mu = new MaterialUnit();
mu.setUnitName("New Unit");
em.persist(mu);

错误是:

Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [50, SEQ_GEN]
Query: ValueReadQuery(name="SEQUENCE" sql="SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?")
Severe:   Local Exception Stack: 
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: No database selected
Error Code: 1046
Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
    bind => [50, SEQ_GEN]
Query: ValueReadQuery(name="SEQUENCE" sql="SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)

那么这种持续运作将如何运作? 任何帮助或建议将不胜感激:)

1 个答案:

答案 0 :(得分:0)

错误表示没有选择数据库..

我认为你的问题就在这一行:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/?"

执行时,它连接到mysql但无法知道你需要什么模式...如果你想拥有多个模式(持久单元),我建议你创建尽可能多的持久单元,就像这样:

<persistence-unit name="PersistenceUnitONE" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.cache.shared.default" value="false"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema1"/>
        <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
    </properties>
</persistence-unit>

<persistence-unit name="PersistenceUnitTWO" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="eclipselink.cache.shared.default" value="false"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/schema2"/>
        <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
    </properties>
</persistence-unit>

然后通过更改此行来调用所需的单位:

emfForTenant = Persistence.createEntityManagerFactory("PersistenceUnitONE");
emfForTenant = Persistence.createEntityManagerFactory("PersistenceUnitTWO");