如何用MySQL配置glassfish4

时间:2020-02-23 14:37:52  来源:igfitidea点击:

本文介绍如何使用Glassfish应用服务器设置和配置MySQL数据库

MySQL目前在生产和开发环境中得到了广泛的应用。glassfish4(和以前的版本)有一个简单的界面来管理数据库连接。我们可以使用Glassfish的管理控制台来设置和配置MySQL数据库,或者如果我们愿意,可以编辑包含数据库首选项的配置文件。我将在本教程中向我们展示这两种方法。本教程也将在Payara上运行。

要求

安装在本地或者远程计算机上的MySQL服务器。此外,我们还需要一个现有用户(带密码)和一个对该用户具有读/写权限的数据库

Glassfish(或者Payara)4应用服务器

在Glassfish上安装MySQL Java连接器

MySQL的官方JDBC(Java数据库连接)驱动程序名为MySQL Connector/Jhttps://dev.mysql.com/downloads/connector/j/

下载TAR或者ZIP归档文件-无论我们喜欢使用什么。解压归档文件后,我们会发现一个名为mysql-connector-java-X.X.XX的JAR文件-垃圾桶(其中X.X.XX是版本号)。

将JAR文件粘贴到

GLASSFISH_HOME/glassfish/bin

创建Glassfish JDBC连接池

打开Glassfish管理控制台本地主机:4848

展开JDBC并单击“JDBC连接池”

单击“新建…”按钮创建新的连接池

Glassfish创建新的JDBC连接池

“输入连接池的名称”

选择

javax.sql.DataSource

作为“资源类型”

选择MySql作为“数据库驱动程序供应商”

点击“下一步”按钮进入下一步

创建JDBC连接池

屏幕# 2勾选“数据源类名”,应设为

com.mysql.jdbc.jdbc2.optional.MysqlDataSource

启用“Ping”

创建JDBC连接池步骤2

现在在同一屏幕上向下滚动。

填写以下必填字段:

输入用户名–数据库

DatabaseName–输入数据库的名称

Password–是我们在第一个字段中输入的用户的密码

URL和URL–之后

jdbc:mysql://:3306/

添加数据库的名称,使其成为

jdbc:mysql://:3306/tutorials

我们可能需要根据项目偏好填写其他字段,这取决于我们!

单击“完成”按钮保存配置。发送到数据库的ping以检查连接。如果一切正常,我们将看到以下消息:

如果我们看到一些错误消息,请单击“编辑”并检查我们输入的数据是否正确。

在Glassfish中创建JDBC资源

创建JDBC资源不一定是MySQL配置教程的一部分,但是我们需要使用它来将项目连接到数据库,所以让我们在这里介绍一下:

选择“JDBC资源”,然后单击“新建…”按钮

Glassfish JDBC资源

在“JNDI Name”下输入

jdbc/

后跟要为此资源指定的名称。例如

jdbc/tutorialsDS

在“池名称”下,选择我们在上一步中刚刚创建的连接池的名称

保存新创建的JDBC资源

Glassfish创建新的JDBC资源

就这样!现在,可以将MySQL数据库与部署在Glassfish服务器上的应用程序一起使用了。

替代配置方式

如果我们希望通过编辑配置文件来创建连接池和JDBC资源,那么可以通过在

<resources>...</resources>

标记到所选域的Glassfish配置文件中

GLASSFISH_HOME/glassfish/domains/domain1/config/domain.xml
<jdbc-connection-pool ping="true" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" name="MySQLPool" res-type="javax.sql.DataSource">
    <property name="SelfDestructOnPingSecondsLifetime" value="0"></property>
    <property name="UseUsageAdvisor" value="false"></property>
    <property name="AllowSlaveDownConnections" value="false"></property>
    <property name="LoadBalanceBlacklistTimeout" value="0"></property>
    <property name="QueryTimeoutKillsConnection" value="false"></property>
    <property name="CacheServerConfiguration" value="false"></property>
    <property name="RoundRobinLoadBalance" value="false"></property>
    <property name="UseCursorFetch" value="false"></property>
    <property name="JdbcCompliantTruncation" value="true"></property>
    <property name="UseOnlyServerErrorMessages" value="true"></property>
    <property name="AllowPublicKeyRetrieval" value="false"></property>
    <property name="DefaultAuthenticationPlugin" value="com.mysql.jdbc.authentication.MysqlNativePasswordPlugin"></property>
    <property name="DontTrackOpenResources" value="false"></property>
    <property name="UseInformationSchema" value="false"></property>
    <property name="UseNanosForElapsedTime" value="false"></property>
    <property name="UseCompression" value="false"></property>
    <property name="EnableEscapeProcessing" value="true"></property>
    <property name="PasswordCharacterEncoding" value="UTF-8"></property>
    <property name="AutoDeserialize" value="false"></property>
    <property name="TcpRcvBuf" value="0"></property>
    <property name="CallableStatementCacheSize" value="100"></property>
    <property name="AutoSlowLog" value="true"></property>
    <property name="IgnoreNonTxTables" value="false"></property>
    <property name="UseJDBCCompliantTimezoneShift" value="false"></property>
    <property name="AllowNanAndInf" value="false"></property>
    <property name="TcpSndBuf" value="0"></property>
    <property name="ContinueBatchOnError" value="true"></property>
    <property name="Logger" value="com.mysql.jdbc.log.StandardLogger"></property>
    <property name="MaxAllowedPacket" value="-1"></property>
    <property name="PrepStmtCacheSqlLimit" value="256"></property>
    <property name="AllowMultiQueries" value="false"></property>
    <property name="StrictFloatingPoint" value="false"></property>
    <property name="PreparedStatementCacheSqlLimit" value="256"></property>
    <property name="CachePreparedStatements" value="false"></property>
    <property name="InitialTimeout" value="2"></property>
    <property name="UseUnicode" value="true"></property>
    <property name="AutoClosePStmtStreams" value="false"></property>
    <property name="UseServerPrepStmts" value="false"></property>
    <property name="IncludeThreadNamesAsStatementComment" value="false"></property>
    <property name="PreparedStatementCacheSize" value="25"></property>
    <property name="CreateDatabaseIfNotExist" value="false"></property>
    <property name="RollbackOnPooledClose" value="true"></property>
    <property name="SocketFactoryClassName" value="com.mysql.jdbc.StandardSocketFactory"></property>
    <property name="LoadBalanceValidateConnectionOnSwapServer" value="false"></property>
    <property name="CompensateOnDuplicateKeyUpdateCounts" value="false"></property>
    <property name="EnablePacketDebug" value="false"></property>
    <property name="JdbcCompliantTruncationForReads" value="true"></property>
    <property name="PadCharsWithSpace" value="false"></property>
    <property name="NoAccessToProcedureBodies" value="false"></property>
    <property name="UseTimezone" value="false"></property>
    <property name="ClientCertificateKeyStoreType" value="JKS"></property>
    <property name="Port" value="3306"></property>
    <property name="ClientInfoProvider" value="com.mysql.jdbc.JDBC4CommentClientInfoProvider"></property>
    <property name="IsInteractiveClient" value="false"></property>
    <property name="CachePrepStmts" value="false"></property>
    <property name="ProfileSQL" value="false"></property>
    <property name="ProfileSql" value="false"></property>
    <property name="MaxQuerySizeToLog" value="2048"></property>
    <property name="LoadBalanceHostRemovalGracePeriod" value="15000"></property>
    <property name="TreatUtilDateAsTimestamp" value="true"></property>
    <property name="DynamicCalendars" value="false"></property>
    <property name="DatabaseName" value="tutorials"></property>
    <property name="UseSSL" value="false"></property>
    <property name="DisconnectOnExpiredPasswords" value="true"></property>
    <property name="DontCheckOnDuplicateKeyUpdateInSQL" value="false"></property>
    <property name="TrustCertificateKeyStoreType" value="JKS"></property>
    <property name="MaxRows" value="-1"></property>
    <property name="SlowQueryThresholdMillis" value="2000"></property>
    <property name="ResultSetSizeThreshold" value="100"></property>
    <property name="UseBlobToStoreUTF8OutsideBMP" value="false"></property>
    <property name="TransformedBitIsBoolean" value="false"></property>
    <property name="Pedantic" value="false"></property>
    <property name="SocksProxyPort" value="1080"></property>
    <property name="UseUltraDevWorkAround" value="false"></property>
    <property name="EnableQueryTimeouts" value="true"></property>
    <property name="LogXaCommands" value="false"></property>
    <property name="PrepStmtCacheSize" value="25"></property>
    <property name="Password" value="YOUR_DB_PASSWORD_HERE"></property>
    <property name="SelfDestructOnPingMaxOperations" value="0"></property>
    <property name="ZeroDateTimeBehavior" value="exception"></property>
    <property name="CallableStmtCacheSize" value="100"></property>
    <property name="SecondsBeforeRetryMaster" value="30"></property>
    <property name="EmulateLocators" value="false"></property>
    <property name="LoginTimeout" value="0"></property>
    <property name="GatherPerfMetrics" value="false"></property>
    <property name="RetriesAllDown" value="120"></property>
    <property name="DetectCustomCollations" value="false"></property>
    <property name="NoDatetimeStringSync" value="false"></property>
    <property name="LoadBalanceStrategy" value="random"></property>
    <property name="UseOldUTF8Behavior" value="false"></property>
    <property name="LoadBalanceAutoCommitStatementThreshold" value="0"></property>
    <property name="UltraDevHack" value="false"></property>
    <property name="UseFastIntParsing" value="true"></property>
    <property name="StrictUpdates" value="true"></property>
    <property name="EmptyStringsConvertToZero" value="true"></property>
    <property name="UseLocalSessionState" value="false"></property>
    <property name="LoggerClassName" value="com.mysql.jdbc.log.StandardLogger"></property>
    <property name="NullNamePatternMatchesAll" value="true"></property>
    <property name="PopulateInsertRowWithDefaultValues" value="false"></property>
    <property name="ProfilerEventHandler" value="com.mysql.jdbc.profiler.LoggingProfilerEventHandler"></property>
    <property name="UseStreamLengthsInPrepStmts" value="true"></property>
    <property name="RunningCTS13" value="false"></property>
    <property name="UseOldAliasMetadataBehavior" value="false"></property>
    <property name="ReadOnlyPropagatesToServer" value="true"></property>
    <property name="SocketFactory" value="com.mysql.jdbc.StandardSocketFactory"></property>
    <property name="MaxReconnects" value="3"></property>
    <property name="ReportMetricsIntervalMillis" value="30000"></property>
    <property name="BlobsAreStrings" value="false"></property>
    <property name="SendFractionalSeconds" value="true"></property>
    <property name="CacheResultSetMetadata" value="false"></property>
    <property name="Paranoid" value="false"></property>
    <property name="AllowUrlInLocalInfile" value="false"></property>
    <property name="NoTimezoneConversionForTimeType" value="false"></property>
    <property name="EmulateUnsupportedPstmts" value="true"></property>
    <property name="ReconnectAtTxEnd" value="false"></property>
    <property name="RequireSSL" value="false"></property>
    <property name="UseHostsInPrivileges" value="true"></property>
    <property name="UseSSPSCompatibleTimezoneShift" value="false"></property>
    <property name="UseReadAheadInput" value="true"></property>
    <property name="ParseInfoCacheFactory" value="com.mysql.jdbc.PerConnectionLRUFactory"></property>
    <property name="DefaultFetchSize" value="0"></property>
    <property name="URL" value="jdbc:mysql://localhost:3306/tutorials"></property>
    <property name="Url" value="jdbc:mysql://localhost:3306/tutorials"></property>
    <property name="AllowMasterDownConnections" value="false"></property>
    <property name="CacheDefaultTimezone" value="true"></property>
    <property name="QueriesBeforeRetryMaster" value="50"></property>
    <property name="FunctionsNeverReturnBlobs" value="false"></property>
    <property name="DumpQueriesOnException" value="false"></property>
    <property name="LoadBalanceExceptionChecker" value="com.mysql.jdbc.StandardLoadBalanceExceptionChecker"></property>
    <property name="VerifyServerCertificate" value="true"></property>
    <property name="NetTimeoutForStreamingResults" value="600"></property>
    <property name="ProcessEscapeCodesForPrepStmts" value="true"></property>
    <property name="UseAffectedRows" value="false"></property>
    <property name="GatherPerformanceMetrics" value="false"></property>
    <property name="TinyInt1isBit" value="true"></property>
    <property name="MetadataCacheSize" value="50"></property>
    <property name="RewriteBatchedStatements" value="false"></property>
    <property name="CacheCallableStatements" value="false"></property>
    <property name="ServerName" value="localhost"></property>
    <property name="GetProceduresReturnsFunctions" value="true"></property>
    <property name="UseGmtMillisForDatetimes" value="false"></property>
    <property name="CapitalizeTypeNames" value="true"></property>
    <property name="ServerConfigCacheFactory" value="com.mysql.jdbc.PerVmServerConfigCacheFactory"></property>
    <property name="NoTimezoneConversionForDateType" value="true"></property>
    <property name="TcpTrafficClass" value="0"></property>
    <property name="AutoGenerateTestcaseScript" value="false"></property>
    <property name="CacheCallableStmts" value="false"></property>
    <property name="FailOverReadOnly" value="true"></property>
    <property name="LoadBalancePingTimeout" value="0"></property>
    <property name="LocatorFetchBufferSize" value="1048576"></property>
    <property name="RelaxAutoCommit" value="false"></property>
    <property name="UseFastDateParsing" value="true"></property>
    <property name="LoadBalanceEnableJMX" value="false"></property>
    <property name="OverrideSupportsIntegrityEnhancementFacility" value="false"></property>
    <property name="HoldResultsOpenOverStatementClose" value="false"></property>
    <property name="InteractiveClient" value="false"></property>
    <property name="UseJvmCharsetConverters" value="false"></property>
    <property name="PortNumber" value="3306"></property>
    <property name="UseDynamicCharsetInfo" value="true"></property>
    <property name="LogSlowQueries" value="false"></property>
    <property name="IncludeThreadDumpInDeadlockExceptions" value="false"></property>
    <property name="ReplicationEnableJMX" value="false"></property>
    <property name="SlowQueryThresholdNanos" value="0"></property>
    <property name="UseDirectRowUnpack" value="true"></property>
    <property name="UseSqlStateCodes" value="true"></property>
    <property name="IncludeInnodbStatusInDeadlockExceptions" value="false"></property>
    <property name="MaintainTimeStats" value="true"></property>
    <property name="PinGlobalTxToPhysicalConnection" value="false"></property>
    <property name="TcpNoDelay" value="true"></property>
    <property name="TraceProtocol" value="false"></property>
    <property name="AlwaysSendSetIsolation" value="true"></property>
    <property name="NullCatalogMeansCurrent" value="true"></property>
    <property name="YearIsDateType" value="true"></property>
    <property name="SocketTimeout" value="0"></property>
    <property name="UseServerPreparedStmts" value="false"></property>
    <property name="UseLocalTransactionState" value="false"></property>
    <property name="GenerateSimpleParameterMetadata" value="false"></property>
    <property name="ExplainSlowQueries" value="false"></property>
    <property name="UseColumnNamesInFindColumn" value="false"></property>
    <property name="ConnectTimeout" value="0"></property>
    <property name="ElideSetAutoCommits" value="false"></property>
    <property name="PacketDebugBufferSize" value="20"></property>
    <property name="RetainStatementAfterResultSetClose" value="false"></property>
    <property name="DumpMetadataOnColumnNotFound" value="false"></property>
    <property name="BlobSendChunkSize" value="1048576"></property>
    <property name="UseLegacyDatetimeCode" value="true"></property>
    <property name="UseUnbufferedInput" value="true"></property>
    <property name="AllowLoadLocalInfile" value="true"></property>
    <property name="ReadFromMasterWhenNoSlaves" value="false"></property>
    <property name="AutoReconnectForPools" value="false"></property>
    <property name="TcpKeepAlive" value="true"></property>
    <property name="ClobberStreamingResults" value="false"></property>
    <property name="User" value="root"></property>
  </jdbc-connection-pool>
  <jdbc-resource pool-name="MySQLPool" jndi-name="jdbc/tutorialsDS"></jdbc-resource>

同时添加以下行

<server>...</server>

标签

<resource-ref ref="jdbc/tutorialsDS"></resource-ref>