最后,我使用了 modeshape-jdbc-store-example 作为代码库。
https://github.com/ModeShape/modeshape-examples/tree/master/modeshape-jdbc-store-example
然后将 infinispan-configuration.xml 和 my-repository-config.json 修改为我的配置。
我还必须修改 pom.xml 以便它可以作为包含所有库的独立 cmd 应用程序运行。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.modeshape.example.jdbcstore.ModeShapeExample</mainClass>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
我添加了一些依赖项,以便我的 SQL 连接可以存储在 JNDI 中:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>naming-factory</artifactId>
<version>5.5.15</version>
</dependency>
<dependency>
<groupId>tomcat</groupId>
<artifactId>naming-resources</artifactId>
<version>5.5.15</version>
</dependency>
将连接放入 JNDI 的代码:
try {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
InitialContext ic = new InitialContext();
ic.createSubcontext("java:");
ic.createSubcontext("java:/jdbc");
final String PROP_DB_NAME = "java:/jdbc/DataDS";
final String PROP_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
final String PROP_CONNECTION_URL = argv[0];
final String PROP_USER = argv[1];
final String PROP_PASWSWORD = argv[2];
// Construct DataSource
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(PROP_DRIVER);
dataSource.setUsername(PROP_USER);
dataSource.setPassword(PROP_PASWSWORD);
dataSource.setUrl(PROP_CONNECTION_URL);
dataSource.setMaxActive(5);
dataSource.setMaxIdle(5);
dataSource.setInitialSize(1);
dataSource.setValidationQuery("SELECT 1");
ic.bind(PROP_DB_NAME, dataSource);
Connection conn = dataSource.getConnection();
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}