gpt4 book ai didi

java - Spark Java 框架中的连接池

转载 作者:搜寻专家 更新时间:2023-10-30 21:11:25 26 4
gpt4 key购买 nike

Java Spark 框架使用嵌入式 Jetty 作为 Web 服务器。Jetty 支持使用 HikariCP 和提供 configuration options在 XML 文件中。然而,根据至 these posts , Spark 允许配置 Jetty。有很多使用 Spark 的例子,但他们要么不使用数据库或使用 DriverManager 连接到数据库。

是否可以通过数据源配置连接池和 Spark 中的 JNDI?如果是,怎么办?

最佳答案

我在 Spark Java 中为 MariaDB 配置了 HikariCP 池。我没有使用 Jetty,而是使用了 Apache Tomcat。以下是一些代码片段:

src/main/resources/mysql-connection.properties

dataSourceClassName=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
dataSource.url=<url>
dataSource.user=<user>
dataSource.password=<password>
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=100
dataSource.prepStmtCacheSqlLimit=2048
dataSource.useServerPrepStmts=true
maximumPoolSize=10

com/example/app/DataSourceFactory.java

public final class DataSourceFactory {

static final Logger LOG = LoggerFactory.getLogger(DataSourceFactory.class);

private static DataSource mySQLDataSource;

private DataSourceFactory() {
}

//returns javax.sql.DataSource
public static DataSource getMySQLDataSource() {
if (mySQLDataSource == null) {
synchronized (DataSourceFactory.class) {
if (mySQLDataSource == null) {
mySQLDataSource = getDataSource("mysql-connection.properties");
}
}
}
return mySQLDataSource;
}

// method to create the DataSource based on configuration
private static DataSource getDataSource(String configurationProperties) {
Properties conf = new Properties();
try {
conf.load(DataSourceFactory.class.getClassLoader().getResourceAsStream(configurationProperties));
} catch (IOException e) {
LOG.error("Can't locate database configuration", e);
}
HikariConfig config = new HikariConfig(conf);
HikariDataSource dataSource = new HikariDataSource(config);
LOG.info("DataSource[" + configurationProperties + "] created " + dataSource);
return dataSource;
}

}

WebContent/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

<display-name>My Spark App</display-name>
<filter>
<filter-name>SparkFilter</filter-name>
<filter-class>spark.servlet.SparkFilter</filter-class>
<init-param>
<param-name>applicationClass</param-name>
<param-value>com.example.app.MySparkApp</param-value>
<!-- MySparkApp implements spark.servlet.SparkApplication -->
</init-param>
</filter>
<filter-mapping>
<filter-name>SparkFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.example.app.AppServletContextListener</listener-class>
</listener>
</web-app>

com/example/app/AppServletContextListener.java

public class AppServletContextListener implements ServletContextListener {

static final Logger LOG = LoggerFactory.getLogger(AppServletContextListener.class);

@Override
public void contextInitialized(ServletContextEvent arg0) {
LOG.info("contextInitialized...");
}

@Override
public void contextDestroyed(ServletContextEvent arg0) {
LOG.info("contextDestroyed...");
try {
if (DataSourceFactory.getMySQLDataSource() != null) {
DataSourceFactory.getMySQLDataSource().unwrap(HikariDataSource.class).close();
}

} catch (SQLException e) {
LOG.error("Problem closing HikariCP pool", e);
}

}

}

最后,您可以通过调用 DataSourceFactory.getMySQLDataSource().getConnection() 获得池化的 java.sql.Connection

关于java - Spark Java 框架中的连接池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42973786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com